# ActiveRecord find\_by\_\*() - how does it work?

**URL:** <https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131>\
**Category:** Ruby\
**Created:** [January 17, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131 "2007-01-17T19:46:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/1 "2007-01-17T19:46:59Z")

</div>

Hi guys!

I’ve been trying to understand how the find\_by\_\*() method on  
ActiveRecord works, but couldn’t find anything that could help. I think  
that you guys can probably help me on that.

On Active Record, you can make a call like  
user.find\_by\_first\_name\_and\_last\_name(“eduardo”…)  
and this will generate the sql to send to the database.

But how does that method gets created? ActiveRecord doesn’t know what I  
want to find until I actually make the call, and the number of different  
combinations of parameters would probably be infinite for it to be  
created during the object initialization, so (I imagine) the method must  
be created **AFTER** it was called.

So my question is, is it possible for an object to receive a call to a  
method that doesn’t exist, not throw a NoMethodError, and respond to  
that call?

I hope my question makes sense! 🙂

Thank you!!!

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/2 "2007-01-17T19:49:56Z")

</div>

Hi –

On Thu, 18 Jan 2007, Eduardo S. wrote:

> But how does that method gets created? ActiveRecord doesn’t know what I  
> want to find until I actually make the call, and the number of different  
> combinations of parameters would probably be infinite for it to be  
> created during the object initialization, so (I imagine) the method must  
> be created **AFTER** it was called.
> 
> So my question is, is it possible for an object to receive a call to a  
> method that doesn’t exist, not throw a NoMethodError, and respond to  
> that call?

Let me be the first of probably 10 or 15 people to say:

method\_missing

🙂

David

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/3 "2007-01-17T19:51:01Z")

</div>

Eduardo S. wrote:

> I hope my question makes sense! 🙂

Your question makes perfect sense and you already figured it out…  
method\_missing() is invoked and it then defines the method Find\_by\_  
depending upon the args you sent it… pretty cool stuff indeed!

ilan

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/4 "2007-01-17T19:54:51Z")

</div>

Thanks, Ilan

By the way, is there any place where I could find a list of all these  
“hooks” that Ruby provides?

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/5 "2007-01-17T19:51:42Z")

</div>

unknown wrote:

> method\_missing
> 
> 🙂
> 
> David

PERFECT!

I imagined that there would exist something like this, but never found  
this method!

So obvious, so useful! 🙂

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/6 "2007-01-17T20:04:46Z")

</div>

Eduardo S. wrote:

> Thanks, Ilan
> 
> By the way, is there any place where I could find a list of all these  
> “hooks” that Ruby provides?

Argghh… .I am always plugging Dave T.'s “Agile Web D. with  
Rails (2nd)” because I think it’s such a great read, gives you  
everything on a silver platter, and has the example you inquired about  
above.

He occasionally posts on this board however and I don’t want to come  
accross as a major suck up so I hope I won’t have to mention this book  
again in order to save the little dignity I have left…

ilan

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/7 "2007-01-17T20:05:01Z")

</div>

Yes, you can write method\_missing method that can handle that… though  
I haven’t dug into the source of Active Record, so it may accomplish the  
goal differently.

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/8 "2007-01-17T20:08:30Z")

</div>

Thanks again Ilan!

I’ll buy the book. 🙂

Thanks, Sammy! Now that I know what to look for, I’ll try to look into  
the ActiveRecord source.

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/9 "2007-01-17T20:20:04Z")

</div>

> again in order to save the little dignity I have left…  
> Well, I have no fear of looking like a suck-up. David Black, “Ruby for  
> Rails,” chapters 13 through 17. Get it, you’ll be happy.

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/10 "2007-01-17T20:43:19Z")

</div>

Thanks a lot, Rob!

---

<div class="post-metadata">

**Author:** ![escoz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3d9bf3/32.png) [@escoz](https://www.ruby-forum.com/u/escoz)\
**Post date:** [January 17, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/activerecord-find-by-how-does-it-work/82131/11 "2007-01-17T20:42:12Z")

</div>

Hi,

On 1/17/07, Eduardo S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But how does that method gets created? ActiveRecord doesn’t know what I  
> want to find until I actually make the call, and the number of different  
> combinations of parameters would probably be infinite for it to be  
> created during the object initialization, so (I imagine) the method must  
> be created **AFTER** it was called.
> 
> So my question is, is it possible for an object to receive a call to a  
> method that doesn’t exist, not throw a NoMethodError, and respond to  
> that call?
> 
> I hope my question makes sense! 🙂

Its a very good question. Here are some references to help:

[http://blog.hasmanythrough.com/2006/08/13/how-dynamic-finders-work](http://blog.hasmanythrough.com/2006/08/13/how-dynamic-finders-work)

> **[Back Double At Ya — err.the\_blog](http://errtheblog.com/post/20)**
>
> Err the Blog: Ruby and Rails from the front.

[http://weblog.jamisbuck.org/2006/11/20/under-the-hood-activerecord-base-find-part-2](http://weblog.jamisbuck.org/2006/11/20/under-the-hood-activerecord-base-find-part-2)  
[http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3](http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3)  
and of course, _Ruby for Rails_.

- Rob
