开发者

Rails: finding a record in the database

I have a Url class with the following attributes in the database:

def self.up
    create_table :urls do |t|
      t.string :url, :null => false
      t.timestamps
    end
end

I would like to find if there is already a record of that URL in the database, but although a request looks like "url"=>{"url"=>"y"} and I do a existing_url = Url.find(params[:url]), I cannot retrieve it, as I get a Unknown key(s): url error.

How can I find wh开发者_StackOverflowether a certain URL is already in the DB?

Cheers! M.


existing_url = Url.find(params[:url][:url])

which is actually should look like this, I believe:

existing_url = Url.find_by_url(params[:url][:url])

or in Rails 3 syntax

existing_url = Url.where(:url => params[:url][:url])


Url.find_by_url(params[:url]) or Url.where(:url => params[:url])

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜