ActiveRecord find_by_*() - how does it work?

E

Eduardo Scoz

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!!!
 
D

dblack

Hi --

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?

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

method_missing

:)


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
I

Ilan Berci

Eduardo said:
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
 
E

Eduardo Scoz

unknown said:
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!
 
E

Eduardo Scoz

Thanks, Ilan

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

Sammy Larbi

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

Ilan Berci

Eduardo said:
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 Thomas's "Agile Web Development 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
 
E

Eduardo Scoz

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

Giles Bowkett

By the way, is there any place where I could find a list of all these
Argghh.. .I am always plugging Dave Thomas's "Agile Web Development 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..

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

Rob Sanheim

Hi,

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

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

http://blog.hasmanythrough.com/2006/08/13/how-dynamic-finders-work
http://errtheblog.com/post/20
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top