ActiveRecord: Behavior not doumented

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

Well, I’m not expert, but from what I’ve been reading you could try
using a dynamic finder such as find_by_ (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

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 H.
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

David Heinemeier H. 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?

[email protected] 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/Base.html#M000860

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

Ray

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


Josh S.
http://blog.hasmanythrough.com