ActiveRecord find_by_*() - how does it work?

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! :slight_smile:

Thank you!!!

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

:slight_smile:

David

Eduardo S. wrote:

I hope my question makes sense! :slight_smile:

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

Thanks, Ilan

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

unknown wrote:

method_missing

:slight_smile:

David

PERFECT!

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

So obvious, so useful! :slight_smile:

Thanks a lot!

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

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.

Thanks again Ilan!

I’ll buy the book. :slight_smile:

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

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.

Thanks a lot, Rob!

Hi,

On 1/17/07, Eduardo S. [email protected] 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! :slight_smile:

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

http://blog.hasmanythrough.com/2006/08/13/how-dynamic-finders-work

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
and of course, Ruby for Rails.

  • Rob