# Comparing polymorphic property to same object fails

**URL:** <https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453>\
**Category:** Rails\
**Created:** [May 6, 2007, 9:15am UTC](https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453 "2007-05-06T09:15:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 6, 2007, 9:15am UTC](https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453/1 "2007-05-06T09:15:07Z")

</div>

I’ve got a polymorphic association on one table. I assign to it in the  
controller.

This is handling invitations of users to tasks in a task management  
application. If the user issuing the invitation is the user being  
invited (ie the user assigned themselves to a task), I want to  
activate the invitation immediately in the model.

The trouble is that this:

```
if self.assignable == self.created_by
  self.activated_at = Time.now
  self.activation_code = nil
end

```

fails to detect equality correctly. I’ve tried ===, eql? etc.

When I look at . **id** when I know that I set assignable equal to  
created\_by, the values are different (wildly so; one is like 4 and the  
other is a five digit number or something).

I’m guessing Rails handles polymorphic associations via a proxy object  
or something. So the question is: how do I do this comparison?

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 6, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453/2 "2007-05-06T16:54:46Z")

</div>

You can get the underlying object from the proxy calling #target or  
#proxy\_target. That should allow you to compare the object ids  
directly.

But why not compare the #id (eg, the actual primary key of the users  
table) of both assignable and created\_by. This is safer especially if  
activerecord decides load created\_by and assignable into two different  
objects which is probably very likely.

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 6, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453/3 "2007-05-06T18:45:56Z")

</div>

On May 6, 9:54 am, eden li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can get the underlying object from the proxy calling #target or  
> #proxy\_target. That should allow you to compare the object ids  
> directly.
> 
> But why not compare the #id (eg, the actual primary key of the users  
> table) of both assignable and created\_by. This is safer especially if  
> activerecord decides load created\_by and assignable into two different  
> objects which is probably very likely.

The problem with comparing just the ids is if the polymorphic  
association is to a different table (ie to a different class), to an  
object that happens to have the same id in that other table.

Unfortunate that if this is documented anywhere, it’s carefully  
hidden. ☹

Thanks.

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 7, 2007, 12:47am UTC](https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453/4 "2007-05-07T00:47:28Z")

</div>

On May 6, 9:54 am, eden li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can get the underlying object from the proxy calling #target or  
> #proxy\_target. That should allow you to compare the object ids  
> directly.

This doesn’t work. Changing the code to:

```
if self.assignable.target == self.created_by
  self.activated_at = Time.now
  self.activation_code = nil
end

```

fails. As does using assignable.proxy\_target.

Anyone know how to get this working?

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 7, 2007, 3:07am UTC](https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453/5 "2007-05-07T03:07:35Z")

</div>

On May 6, 5:46 pm, Gisborne [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> self.activation_code = nil
> end
> 
> ```
> 
> fails. As does using assignable.proxy\_target.
> 
> Anyone know how to get this working?

Whoops. Turns out it works fine and I was just being a complete moron.  
Thanks for the help.

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 7, 2007, 5:02am UTC](https://www.ruby-forum.com/t/comparing-polymorphic-property-to-same-object-fails/103453/6 "2007-05-07T05:02:40Z")

</div>

On May 7, 12:44 am, Gisborne [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The problem with comparing just the ids is if the polymorphic  
> association is to a different table (ie to a different class), to an  
> object that happens to have the same id in that other table.

Maybe I misunderstand your schema, but after retrieving “assignable”  
you should get a proper user object. The polymorphic association will  
retrieve the object from the right table for you. This is one of the  
reasons you need an :assignable\_type column.

> Unfortunate that if this is documented anywhere, it’s carefully  
> hidden. ☹

The association proxies are in the guts of ActiveRecord. I assume  
it’s not well documented because rails-core wants to be able to make  
massive changes to it between releases. Anyway, you only need to deal  
with them if you’re relying on strict ruby object semantics.
