# ActiveRecord: Behavior not doumented

**URL:** <https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787>\
**Category:** Rails\
**Created:** [April 10, 2006, 11:39am UTC](https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787 "2006-04-10T11:39:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2006, 11:39am UTC](https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787/1 "2006-04-10T11:39:36Z")

</div>

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](http://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](http://rubyonrails.org) out of sync?

## Greetings,

Daniel Völkerts  
Protected by Anti Pesto. – Wallace & Gromit

* * *

Gesendet durch IMP von [mail.voelkerts.de](http://mail.voelkerts.de)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787/2 "2006-04-10T16:06:33Z")

</div>

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](http://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](http://rubyonrails.org) out of sync?
> 
> ## Greetings,
> 
> Daniel Vï¿½lkerts  
> Protected by Anti Pesto. – Wallace & Gromit
> 
> * * *
> 
> Gesendet durch IMP von [mail.voelkerts.de](http://mail.voelkerts.de)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787/3 "2006-04-10T16:18:09Z")

</div>

> 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](http://www.loudthinking.com) – Broadcasting Brain  
[http://www.basecamphq.com](http://www.basecamphq.com) – Online project management  
[http://www.backpackit.com](http://www.backpackit.com) – Personal information manager  
[http://www.rubyonrails.com](http://www.rubyonrails.com) – Web-application framework

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787/4 "2006-04-10T17:32:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787/5 "2006-04-10T16:30:22Z")

</div>

removed\_email\_address@domain.invalid wrote:

> the db. Is the doc on [rubyonrails.org](http://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](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000860)

I think you want #find(an\_id, :limit =\> 1)

–

Ray

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 10, 2006, 9:10pm UTC](https://www.ruby-forum.com/t/activerecord-behavior-not-doumented/93787/6 "2006-04-10T21:10:03Z")

</div>

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

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
