# Is it magic?

**URL:** <https://www.ruby-forum.com/t/is-it-magic/115204>\
**Category:** Rails\
**Created:** [September 21, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/is-it-magic/115204 "2007-09-21T17:58:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [September 21, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/is-it-magic/115204/1 "2007-09-21T17:58:18Z")

</div>

Ok, I have a silly question. How does Rails know to generate SQL from  
this:

@comment = @post.comments.find(params[:id])

If @post.comments.class is Array then how does Array know to write SQL  
with something like my\_array.find(1)?

Does Rails add something to Ruby’s Array class to handle that?

In any case it works. I am just curious how it does.

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [September 21, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/is-it-magic/115204/2 "2007-09-21T18:10:20Z")

</div>

Never mind, I get it now. It’s has\_many that adds the needed  
collection methods to generate the needed SQL.

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [September 25, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/is-it-magic/115204/3 "2007-09-25T12:56:55Z")

</div>

On 9/21/07, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Never mind, I get it now. It’s has\_many that adds the needed  
> collection methods to generate the needed SQL.

Robert, theres a lot more to it than that. Enough more that  
you were nearly right in the subject line.

I don’t know the full details, but Find does not return an Array.  
It returns a Proxy object, which looks like (quacks like) an  
Array.

ActiveRecord will also add dynamic finders at runtime, with some  
more method\_missing? cleverness. Its probably well worth your  
time to study ActiveRecord in detail. Rails gets most of its magic  
from it, and you will find a lot of solid resources behind this.

Also ActiveRecord can be used outside of Rails and very easily too;  
making it a useful general purpose tool for even ad-hoc scripting.
