how does rails do this "find_by_?"

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