[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ActiveRecord find_by_*() - how does it work?

Eduardo Scoz

1/17/2007 6:47:00 PM

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

10 Answers

dblack

1/17/2007 6:50:00 PM

0

Ilan Berci

1/17/2007 6:51:00 PM

0

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

Eduardo Scoz

1/17/2007 6:52:00 PM

0

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

Eduardo Scoz

1/17/2007 6:55:00 PM

0

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

Sammy Larbi

1/17/2007 7:04:00 PM

0

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


Ilan Berci

1/17/2007 7:05:00 PM

0

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

Eduardo Scoz

1/17/2007 7:09:00 PM

0

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

Giles Bowkett

1/17/2007 7:19:00 PM

0

> > 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.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...

Rob Sanheim

1/17/2007 7:41:00 PM

0

Hi,

On 1/17/07, Eduardo Scoz <escoz@msn.com> 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! :)

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

http://blog.hasmanythrough.com/2006/08/13/how-dynamic-fi...
http://errtheblog.c...
http://weblog.jamisbuck.org/2006/11/20/under-the-hood-activerecord-base-f...
http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-f...
and of course, _Ruby for Rails_.

- Rob

Eduardo Scoz

1/17/2007 7:44:00 PM

0

Thanks a lot, Rob!


--
Posted via http://www.ruby-....