Forum: Ruby on Rails ActiveRecord: Behavior not doumented

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
89dc62dd0a40f43a75d73e8059d61a91?d=identicon&s=25 unknown (Guest)
on 2006-04-10 11:39
(Received via mailing list)
Hello everbody, doing a

#find(:first,an_id)

with Rails 1.1.0 I expected that find returns the record which id
mathches the
given parameter an_id or nil if it couldn't be found.

This behaviour is documented on api.rubyonrails.org.

But the find returns the first available object and not nil if an_id is
not in
the db. Is the doc on rubyonrails.org out of sync?


Greetings,
--
Daniel Völkerts
Protected by Anti Pesto. -- Wallace & Gromit

---
Gesendet durch IMP von mail.voelkerts.de
7e7d5611762251b7b1939a7c7b01d648?d=identicon&s=25 Angus McDonald (Guest)
on 2006-04-10 16:06
Well, I'm not expert, but from what I've been reading you could try
using a dynamic finder such as find_by_<fieldname> (e.g. find_by_an_id,
or find_by_id if you're using a standard id field). The dynamic finders
will return NULL if they cannot find the record. The normal find will
give you an error that you can rescue, which is the other way of
handling this gracefully.

unknown wrote:
> Hello everbody, doing a
>
> #find(:first,an_id)
>
> with Rails 1.1.0 I expected that find returns the record which id
> mathches the
> given parameter an_id or nil if it couldn't be found.
>
> This behaviour is documented on api.rubyonrails.org.
>
> But the find returns the first available object and not nil if an_id is
> not in
> the db. Is the doc on rubyonrails.org out of sync?
>
>
> Greetings,
> --
> Daniel V�lkerts
> Protected by Anti Pesto. -- Wallace & Gromit
>
> ---
> Gesendet durch IMP von mail.voelkerts.de
6edd67c92a1dab5eb23fed79f3c18564?d=identicon&s=25 David Heinemeier Hansson (Guest)
on 2006-04-10 16:18
(Received via mailing list)
> Hello everbody, doing a
>
> #find(:first,an_id)

That won't work. You need either #find(:first, options), which will
return the first record that matches does options, or #find(id), which
will return the record of that id or raise an exception if not found.
You can't combine them.
--
David Heinemeier Hansson
http://www.loudthinking.com -- Broadcasting Brain
http://www.basecamphq.com   -- Online project management
http://www.backpackit.com   -- Personal information manager
http://www.rubyonrails.com  -- Web-application framework
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-04-10 16:30
(Received via mailing list)
daniel@voelkerts.de wrote:
> the db. Is the doc on rubyonrails.org out of sync?
There are three forms of find. You have conflated two of them.

#find(:first,an_id) when an_id is a numerical value returns the first
record in the db no matter what the value of an_id is.

This is documented

http://api.rubyonrails.com/classes/ActiveRecord/Ba...

I think you want #find(an_id, :limit => 1)

--

Ray
C70264c74698cbe2324b91ff778abcff?d=identicon&s=25 Daniel Völkerts (Guest)
on 2006-04-10 17:32
(Received via mailing list)
David Heinemeier Hansson schrieb:
>> Hello everbody, doing a
>>
>> #find(:first,an_id)
>
> That won't work. You need either #find(:first, options), which will
> return the first record that matches does options, or #find(id), which
> will return the record of that id or raise an exception if not found.
> You can't combine them.

Aaah, yes. Thank you a lot. What is the rubish way of doing this? As I
understand I can write:

a_object.find(:first,:conditions=>["id=?",id])

which will return the Object with id or I can wrote a fixed version


class a_object << ActiveRecord::Base

  def a_Object::find_or_nil(id)
   begin
     find(id)
   rescue ActiveRecord::NoMethodError
     nil
   end
  end

end

 From the view of coolness I prefer the first, any comments?
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-04-10 21:10
Daniel Völkerts wrote:
> Aaah, yes. Thank you a lot. What is the rubish way of doing this? As I
> understand I can write:
>
> a_object.find(:first,:conditions=>["id=?",id])
>
> which will return the Object with id or I can wrote a fixed version
>
>
> class a_object << ActiveRecord::Base
>
>   def a_Object::find_or_nil(id)
>    begin
>      find(id)
>    rescue ActiveRecord::NoMethodError
>      nil
>    end
>   end
>
> end
>
>  From the view of coolness I prefer the first, any comments?

Thing.find_by_id(thing_id)

That will return the thing with the provided id, or nil if there isn't
one. It's just one of the standard dynamic finders you get with an
ActiveRecord class. The same thing hold for any other attributes of the
class too. Read the section on"Dynamic attribute-based finders" in
http://api.rubyonrails.org/classes/ActiveRecord/Base.html

--
Josh Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.