How does rails do this "find_by_?"

Hello there,

I’m wondering how do they create functions on the fly?

find_by_any_column_name ??

I want to do something similar with my models, but I don’t really know
how to do it?

user_current_nickname
user_current_id
user_current_role

etc.?

Regards,
Jamal

Jamal S. wrote:

Hello there,

I’m wondering how do they create functions on the fly?

find_by_any_column_name ??

I want to do something similar with my models, but I don’t really know
how to do it?

user_current_nickname
user_current_id
user_current_role

etc.?

Regards,
Jamal

I found out, you should use the method_missing method implemented in
Ruby.

DAMN I LOVE RUBY, it has every solution for you :smiley:

http://duncan.mac-vicar.com/blog/archives/18

Jamal,

The find_by methods are illusions; they don’t actually exist.

What you’re looking for is method_missing(name, args). When you
attempt to send a message (call a method on) an instance of an object
that doesn’t exist, method_missing is called instead. You can then
decide behavior based on name and args.

Hope this helps.

Sean

Hi,

The find_by methods are illusions; they don’t actually exist.

When you attempt to send a message (call a method on) an instance of an object that doesn’t exist, method_missing is called instead. You can then decide behavior based on name and args.

Well… the above is true, but it’s not the whole truth :wink:

method_missing is called when you invoke a non-existing method for an
object. By checking the parameters you receive, you can then implement
the behaviour you want. That would be perfect except for one thing. It
slows down the calls to those “virtual” methods.

fortunately, ruby is pretty dynamic and it allows to make nice things,
like creating methods on the fly for a given object/class (I know
classes are objects, but just to make it clear).

In order to make the find_by_whatever efficient, the first time a
find_by_something method is called method missing will be invoked, yes…
but what it does is creating on the fly a new method with that
signature, so next time you use the method, it will be directly invoked,
and the method_missing will be skipped entirely.

cool, uh?

regards,

javier ramirez

Estamos de estreno… si necesitas llevar el control de tus gastos
visita http://www.gastosgem.com !!Es gratis!!

Sean-Paul Rees wrote:

Jamal,

The find_by methods are illusions; they don’t actually exist.

What you’re looking for is method_missing(name, args). When you
attempt to send a message (call a method on) an instance of an object
that doesn’t exist, method_missing is called instead. You can then
decide behavior based on name and args.

Hope this helps.

Sean

http://duncan.mac-vicar.com/blog/archives/18

Is described there :slight_smile: but thanks anyway :smiley:

Alain R. wrote:

I’m wondering how do they create functions on the fly?

see:
Under the hood: ActiveRecord::Base.find
http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3

Alain

Thank you very much, thats great help to :slight_smile:

I’m wondering how do they create functions on the fly?

see:
Under the hood: ActiveRecord::Base.find
http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3

Alain