Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?
David M. wrote:
Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?
Model.find(:first)
or Model.find(:all).last
Thanks, but this is not what I am looking for. There is a possibility
that the record I am searching for has been destroyed. For example,
user with id = 1 has been deleted, but they posted a comment. I still
want to display the comment and I want to show the users name if they
still exist. Is there a method that looks for that id and returns the
appropriate one if found and returns nil if it doesn’t exist without
crashing?
Jeremy W. wrote:
David M. wrote:
Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?Model.find(:first)
or Model.find(:all).last
David M. wrote:
Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?
Model.find_by_id(1)
David M. wrote:
Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?
The #find method will do what you ask if you call it the right way:
User.find(1) # raises error if no user with ID 1
User.find(:first, :conditions => { :id => 1 }) # returns nil if no user
with ID 1
User.find(:all, :conditions => { :id => 1 }) # returns empty array if
no user with ID 1
And the dynamic finder does what you want too:
User.find_by_id(1) # returns nil if no user with ID 1
User.find_all_by_id(1) # returns empty array if no user with ID 1
I’ve never been a fan of the kitchen-sink nature of the #find method. It
seems like three different methods to me.
–
Josh S.
http://blog.hasmanythrough.com
Yep, just rescue from the exception if it occurs like this
begin
@m = Model.find(id)
rescue ActiveRecord::RecordNotFound
#do something here if you want to
end
if @m
#do stuff with @m
else
#do stuff without @m
end
Hope this helps ya.
David M. wrote:
Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?
foo = Model.find(id) rescue nil
–Al Evans
On Sun, 2007-16-09 at 02:53 +0200, David M. wrote:
Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?
Try: model.find_by_id(id)
–
Rick
[email protected]
Danny is correct as well, both will work depending on what you are
trying to do. I had forgotten that find_by_* dynamic finders returned
nil.
Personally I end up using this option a good deal:
User.find(:first, :options=>{:id=> 1})
When you know that you’re looking for just one instance it’s simply
easier to test for nil than to test for an empty array.
On Sep 16, 7:18 pm, Josh S. [email protected]
Danny B. wrote in post #554031:
David M. wrote:
Is there a method to use instead of model.find(id) that returns nil if
nothing is found - meaning there is no id that matches?Model.find_by_id(1)
Worked perfectly for me! Thanks!