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

**URL:** <https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337>\
**Category:** Rails\
**Created:** [March 27, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337 "2007-03-27T23:49:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 27, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337/1 "2007-03-27T23:49:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 28, 2007, 12:02am UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337/2 "2007-03-28T00:02:53Z")

</div>

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 😃

[http://duncan.mac-vicar.com/blog/archives/18](http://duncan.mac-vicar.com/blog/archives/18)

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 28, 2007, 12:37am UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337/3 "2007-03-28T00:37:25Z")

</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:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 28, 2007, 9:38am UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337/4 "2007-03-28T09:38:26Z")

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

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

## –

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

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 28, 2007, 9:26am UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337/5 "2007-03-28T09:26:01Z")

</div>

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](http://duncan.mac-vicar.com/blog/archives/18)

Is described there 🙂 but thanks anyway 😃

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 28, 2007, 10:34am UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337/6 "2007-03-28T10:34:51Z")

</div>

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](http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3)
> 
> Alain

Thank you very much, thats great help to 🙂

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 28, 2007, 9:48am UTC](https://www.ruby-forum.com/t/how-does-rails-do-this-find-by/90337/7 "2007-03-28T09:48:27Z")

</div>

> 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](http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3)

Alain
