Reputation: 1290
When using find_by
and just supplying an id param, find_by
returns the first entry in the table.
E.g.
@article = Article.find_by(params[:article_id])
returns article with id = 1
while using find
gives me the article with id = :article_id
Can somebody explain why find_by returns the record with the first id?
Upvotes: 1
Views: 1096
Reputation: 149
You can also try @article = Article.find_by(:id => params[:article_id])
Upvotes: 0
Reputation: 3427
Using the find
method, you can retrieve the object corresponding to the specified primary key that matches any supplied options.
so this is correct syntax
@article = Article.find(params[:article_id])
while The find_by
method finds the first record matching some conditions
so you should write
@article = Article.find_by(id:params[:article_id])
source:http://guides.rubyonrails.org/active_record_querying.html
Upvotes: 5