# how does rails do this "find\_by\_?"

**URL:** <https://discuss.rubyonrails.org/t/how-does-rails-do-this-find-by/10873>\
**Category:** rubyonrails-talk\
**Created:** [March 27, 2007, 9:56pm UTC](https://discuss.rubyonrails.org/t/how-does-rails-do-this-find-by/10873 "2007-03-27T21:56:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean-Paul\_Rees](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Sean-Paul\_Rees](https://discuss.rubyonrails.org/u/Sean-Paul_Rees)\
**Post date:** [March 27, 2007, 9:56pm UTC](https://discuss.rubyonrails.org/t/how-does-rails-do-this-find-by/10873/1 "2007-03-27T21:56:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![javier\_ramirez](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@javier\_ramirez](https://discuss.rubyonrails.org/u/javier_ramirez)\
**Post date:** [March 28, 2007, 7:36am UTC](https://discuss.rubyonrails.org/t/how-does-rails-do-this-find-by/10873/2 "2007-03-28T07:36:58Z")

</div>

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. &nbsp;&nbsp;

Well.. the above is true, but it's not the whole truth 😉

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
