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!!!
···
--
Posted via http://www.ruby-forum.com/.
Eduardo Scoz 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
···
--
Posted via http://www.ruby-forum.com/\.
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.
Eduardo Scoz wrote:
···
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!!!
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://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
···
On 1/17/07, Eduardo Scoz <escoz@msn.com> wrote:
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!
···
--
Posted via http://www.ruby-forum.com/\.
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.
···
--
Posted via http://www.ruby-forum.com/.
Thanks, Ilan
By the way, is there any place where I could find a list of all these
"hooks" that Ruby provides?
···
--
Posted via http://www.ruby-forum.com/.
Eduardo Scoz 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 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
···
--
Posted via http://www.ruby-forum.com/\.
> 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..
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.
···
--
Giles Bowkett
http://www.gilesgoatboy.org
http://gilesgoatboy.blogspot.com