# Model: self is not child's parent

**URL:** <https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436>\
**Category:** Rails\
**Created:** [May 17, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436 "2007-05-17T22:12:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 17, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/1 "2007-05-17T22:12:08Z")

</div>

Assume a model Parent and another model Child. Child belongs\_to Parent  
and Parent has\_many Children. The following fails:

@  
p = Parent.find(:first)  
assert p.equal?(p.children[0].parent)  
@

It seems odd to me that I and my child’s parent are not the same object.  
In fact, this is royally screwing me up. Can anyone tell me why this is  
or point me at a relevant discussion, blog posting, or bug that refers  
to this.

Thanks!  
-Ben

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 17, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/2 "2007-05-17T22:13:17Z")

</div>

Ignore the spurious at-signs

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 18, 2007, 2:06am UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/3 "2007-05-18T02:06:55Z")

</div>

On 18/05/2007, at 6:12 AM, Ben H. wrote:

> object.  
> In fact, this is royally screwing me up. Can anyone tell me why  
> this is  
> or point me at a relevant discussion, blog posting, or bug that refers  
> to this.

ActiveRecord (at least in Rails 1.2.3) doesn’t do object caching, so  
following the relationships down and back up again will give you a  
new object. However:

```
 assert p == p.children[0].parent

```

should still succeed.

Pete Y.

> **[Pete Yandell — Principal Engineer / Architect](https://notahat.com/)**
>
> Pete Yandell is an experienced Principal Software Engineer and Architect based in Melbourne, Australia.

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 18, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/4 "2007-05-18T15:26:24Z")

</div>

a.equal?(b) iff a is the same object as b

The eql? method returns true if obj and anObject have the same value

On 5/17/07, Pete Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > assert p.equal?(p.children[0].parent)  
> > following the relationships down and back up again will give you a

–  
Amos K.  
Ramped Media  
USPS  
Programmer/Analyst  
St. Louis, MO  
Looking for something to do? Visit [ImThere.com](http://ImThere.com)

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 18, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/5 "2007-05-18T23:27:27Z")

</div>

Seems to work perfectly for me.

parent.rb  
class Parent \< ActiveRecord::Base  
has\_many :children  
end

child.rb  
class Child \< ActiveRecord::Base  
belongs\_to :parent  
end

parent\_test.rb  
def test\_child\_parent\_is\_parent  
parent = Parent.find(:first)  
assert\_equal parent, parent.children[0].parent  
end

003\_load\_test\_data.rb  
class LoadTestData \< ActiveRecord::Migration  
def self.up  
parent = Parent.create(:name =\> “Robert”)  
Child.create(:name =\> “Bill”, :parent =\> parent)  
Child.create(:name =\> “Sally”, :parent =\> parent)  
end

def self.down  
Child.delete\_all  
Parent.delete\_all  
end  
end

Test results:  
Rigel$ ruby test/unit/parent\_test.rb  
Loaded suite test/unit/parent\_test  
Started  
.  
Finished in 0.10244 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

On May 17, 4:13 pm, Ben H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 19, 2007, 12:07am UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/6 "2007-05-19T00:07:32Z")

</div>

A bit more detail too offer…

> From the ri on equal?:

* * *

## Object#equal? obj == other =\> true or false obj.equal?(other) =\> true or false obj.eql?(other) =\> true or false

```
 Equality---At the +Object+ level, +==+ returns +true+ only if

```

_obj_  
and _other_ are the same object. Typically, this method is  
overridden in descendent classes to provide class-specific  
meaning.

```
 Unlike +==+, the +equal?+ method should never be overridden by
 subclasses: it is used to determine object identity (that is,
 +a.equal?(b)+ iff +a+ is the same object as +b+).

 The +eql?+ method returns +true+ if _obj_ and _anObject_ have the
 same value. Used by +Hash+ to test members for equality. For
 objects of class +Object+, +eql?+ is synonymous with +==+.
 Subclasses normally continue this tradition, but there are
 exceptions. +Numeric+ types, for example, perform type conversion
 across +==+, but not across +eql?+, so:

    1 == 1.0 #=> true
    1.eql? 1.0 #=> false

```

* * *

And both these assertions pass just fine:  
def test\_child\_parent\_is\_parent  
parent = Parent.find(:first)  
assert\_equal(parent, parent.children[0].parent)  
assert(parent.eql?(parent.children[0].parent))  
end

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 19, 2007, 1:13am UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/7 "2007-05-19T01:13:16Z")

</div>

Thanks for answering my question. I suspected the complexity involved  
in ensuring that self and child’s parent are not just the same database  
row but also the same instance was prohibitive. Looks like I was right.

-Ben

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 18, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/8 "2007-05-18T23:39:28Z")

</div>

But you are correct that this is two separate instances of the Parent  
object. Rails does not seem to have a uniquing system so you’ll need  
to compare object equality using methods supplied by ActiveRecord,  
which is how “assert\_equal” is going to determine equality.

Basically, you have two separate instances of Parent representing the  
same row in the parents table. Building a system that ensures object  
equality in object relational mapping is a pretty complex endeavor.  
It appears that Rails makes no attempt to do this.

---

<div class="post-metadata">

**Author:** ![benhiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@benhiller](https://www.ruby-forum.com/u/benhiller)\
**Post date:** [May 22, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/model-self-is-not-childs-parent/104436/9 "2007-05-22T20:06:13Z")

</div>

you want.

assert p.eql? not p.equal? (equal is more restrictive/exact than  
eql?).

I’m not entirely sure, but I believe when you call  
p.children[0].parent you are actually instantiating in memory an new  
object of Parent class. So, #\<Parent:0x39f3018 \> is exactly equal to  
#\<Parent:0x39f3020 \>

so in the end use == or eql? instead of equal? if your not wanting to  
check for EXACTLY the same instance of Parent.

Hope that helps,  
Jim

> **[Equality in Ruby - == eql? and equal?](http://www.wellho.net/mouth/985_Equality-in-Ruby-eql-and-equal-.html)**
>
> Equality in Ruby - == eql? and equal?

On May 17, 3:13 pm, Ben H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
