# Understanding Polymorphicness

**URL:** <https://www.ruby-forum.com/t/understanding-polymorphicness/127399>\
**Category:** Rails\
**Created:** [January 28, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/understanding-polymorphicness/127399 "2008-01-28T22:21:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 28, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/understanding-polymorphicness/127399/1 "2008-01-28T22:21:03Z")

</div>

Rating Model - belongs\_to :ratable, :polymorphic =\> true  
User Model & Comment Model & Picture Model - has\_one :rating, :as  
=\> :ratable

Okay, so now my “Ratings” table should have these columns:  
ups = 5  
downs = 2  
ratable\_id = 3  
ratable\_type = “user”

And that will automatically make user # 3 have user.rating.ups = 5  
right? And the ratable\_type will always use the singlular form, right?

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 28, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/understanding-polymorphicness/127399/2 "2008-01-28T23:48:29Z")

</div>

Anyone know if this is the best way to do this?

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 29, 2008, 1:17am UTC](https://www.ruby-forum.com/t/understanding-polymorphicness/127399/3 "2008-01-29T01:17:20Z")

</div>

> And that will automatically make user # 3 have user.rating.ups = 5  
> Yes

> right? And the ratable\_type will always use the singlular form, right?  
> Yes

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 31, 2008, 12:51am UTC](https://www.ruby-forum.com/t/understanding-polymorphicness/127399/4 "2008-01-31T00:51:21Z")

</div>

For some reason this isn’t working.

I have done all that I stated above, then I made an object Rating with  
a ratable\_id = 6, ratable\_type = “web\_page”

But WebPages.find(6).rating returns a Nil. Ideas?

On Jan 28, 6:17 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 31, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/understanding-polymorphicness/127399/5 "2008-01-31T19:21:51Z")

</div>

Not sure if this is the problem, but the descrimiator column  
(ratable\_type in this case) should have a value that matches the camel  
cased class name. In your original post it looks like you have a  
lowercase ‘u’ (user) instead of uppercase (“User”).

Looking at a later post this appears to be the case. You should have  
“WebPage” as the ratable\_type, not “web\_page”.

HTH,  
AndyV

PS: Any job posting dissections you want to share 🙂

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 31, 2008, 3:02am UTC](https://www.ruby-forum.com/t/understanding-polymorphicness/127399/6 "2008-01-31T03:02:14Z")

</div>

Does there need to be a middle table or something with Polymorphic? I  
didn’t see any mention of it.

-Ryan
