# Model Magic

**URL:** <https://www.ruby-forum.com/t/model-magic/186930>\
**Category:** Rails\
**Created:** [April 14, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/model-magic/186930 "2010-04-14T15:20:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![algnod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@algnod](https://www.ruby-forum.com/u/algnod)\
**Post date:** [April 14, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/model-magic/186930/1 "2010-04-14T15:20:25Z")

</div>

> > [].eql? @product.files  
> > =\> false
> 
> > @product.files.eql? []  
> > =\> true

version: 2.3.5  
Product :has\_many files

---

<div class="post-metadata">

**Author:** ![algnod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@algnod](https://www.ruby-forum.com/u/algnod)\
**Post date:** [April 14, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/model-magic/186930/2 "2010-04-14T15:21:35Z")

</div>

On Apr 14, 2:06 pm, Alexander [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > [].eql? @product.files  
> > > =\> false  
> > > @product.files.eql? []

You’ve uncovered Active Record’s terrible secret, files isn’t actually  
an Array.

Fred

---

<div class="post-metadata">

**Author:** ![algnod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@algnod](https://www.ruby-forum.com/u/algnod)\
**Post date:** [April 14, 2010, 3:55pm UTC](https://www.ruby-forum.com/t/model-magic/186930/3 "2010-04-14T15:55:34Z")

</div>

> You’ve uncovered Active Record’s terrible secret, files isn’t actually  
> an Array.

Actually I’d call it magic or voodoo, but not a dirty secret.

It’s actually a brilliant/necessary part of the arel features in Rails  
3.  
When you select a recordset using Rails 3 arel features (where, order,  
etc), it doesn’t actually call the DB until you iterate over the  
resultset.  
This means if that part of the view is cached, it won’t actually hit  
your  
DB at all. If files was an array it would have had to call the DB to  
fill  
the array, only for it not to be used.

So, the OP’s code snippet is not surprising - @product.files is not an  
array, but if you use the eql? method on that object it will compare  
it’s  
internal data to an empty array (but arrays know nothing about this  
ActiveRecord class).

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![algnod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@algnod](https://www.ruby-forum.com/u/algnod)\
**Post date:** [April 15, 2010, 9:52am UTC](https://www.ruby-forum.com/t/model-magic/186930/4 "2010-04-15T09:52:47Z")

</div>

Yes, I know about this AR behavior.  
I think, would be correct if the comparison works both ways.  
For beginners, it looks like a bug.

---

<div class="post-metadata">

**Author:** ![algnod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@algnod](https://www.ruby-forum.com/u/algnod)\
**Post date:** [April 15, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/model-magic/186930/5 "2010-04-15T16:48:28Z")

</div>

> > Yes, I know about this AR behavior.  
> > I think, would be correct if the comparison works both ways.  
> > For beginners, it looks like a bug.
> 
> Is it really worth monkeypatching Array#eql? for this? I think not.

I almost posted the same thing earlier, but had a second thought.

Would it not be easier to create a to\_a method on the ActiveRecord  
resultset  
object (whatever that is)?

It’s not going to be as efficient as it will then (at that point) need  
to  
instantiate all result objects to compare, but it would then work both  
ways  
round…

But I didn’t get a chance to look further in to it to test the  
theory/idea.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![algnod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@algnod](https://www.ruby-forum.com/u/algnod)\
**Post date:** [April 15, 2010, 4:24pm UTC](https://www.ruby-forum.com/t/model-magic/186930/6 "2010-04-15T16:24:35Z")

</div>

Alexander G. wrote:

> Yes, I know about this AR behavior.  
> I think, would be correct if the comparison works both ways.  
> For beginners, it looks like a bug.

Is it really worth monkeypatching Array#eql? for this? I think not.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
