Forum: Ruby ActiveRecord find_by_*() - how does it work?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eduardo S. (Guest)
on 2007-01-17 20:46
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!!!
unknown (Guest)
on 2007-01-17 20:49
(Received via mailing list)
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
Ilan B. (Guest)
on 2007-01-17 20:51
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
Eduardo S. (Guest)
on 2007-01-17 20:51
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!
Eduardo S. (Guest)
on 2007-01-17 20:54
Thanks, Ilan

By the way, is there any place where I could find a list of all these
"hooks" that Ruby provides?
Ilan B. (Guest)
on 2007-01-17 21:04
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
Sammy L. (Guest)
on 2007-01-17 21:05
(Received via mailing list)
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 S. (Guest)
on 2007-01-17 21:08
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.
Giles B. (Guest)
on 2007-01-17 21:20
(Received via mailing list)
> 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.
Rob S. (Guest)
on 2007-01-17 21:42
(Received via mailing list)
Hi,

On 1/17/07, Eduardo S. <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-dyna...
http://errtheblog.com/post/20
http://weblog.jamisbuck.org/2006/11/20/under-the-h...
http://weblog.jamisbuck.org/2006/12/1/under-the-ho...
and of course, _Ruby for Rails_.

- Rob
Eduardo S. (Guest)
on 2007-01-17 21:43
Thanks a lot, Rob!
This topic is locked and can not be replied to.