# "Owning" a polymorphic

**URL:** <https://www.ruby-forum.com/t/owning-a-polymorphic/56461>\
**Category:** Rails\
**Created:** [March 31, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/owning-a-polymorphic/56461 "2006-03-31T13:01:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [March 31, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/owning-a-polymorphic/56461/1 "2006-03-31T13:01:14Z")

</div>

I’m playing around with RoR and I’m having trouble figuring out how to  
describe a certain relationship. I’m building a social networking site  
where you have users who can comment on other users, photos, videos, and  
blogs. I’ve figured out how to have a comment belong to a user or media  
item through a polymorphic relationships but I dont’ know how to have  
the comment also be “owned” by the user who made the comment. I’ve  
included some code to help with the description:

#User class  
class User \< ActiveRecord::Base  
has\_many :photos  
has\_many :blogs  
…  
end

#photo (also blog and video are similar)  
class Photo \< ActiveRecord::Base  
belongs\_to :user  
has\_many :comments, :as =\> :commentable  
end

#comment class  
class Comment \< ActiveRecord::Base  
belongs\_to :commentable, :polymorphic =\> true  
end

#comment table includes the following fields:

id  
content \<— the actual comment text  
user\_id \<— the user who made the comment  
commentable\_id  
commentable\_type

I’ve thought this over for 2 days and I still can’t seem to figure out a  
way to describe what I want to describe… Does anyone have any ideas or  
suggestions on how to do this?

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [March 31, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/owning-a-polymorphic/56461/2 "2006-03-31T13:12:18Z")

</div>

Maybe if you have an intermediate table, which links one-to-one to the  
users (maybe called “commentor”), then each comment can link to that?  
It’s a little clunky, but less so than any alternative I can think so.  
That’s the problem with rails’ strict naming conventions… if you  
want to create more than one link between the same two tables, you’re  
a bit stuck. Hope this helps.  
-Nathan

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [March 31, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/owning-a-polymorphic/56461/3 "2006-03-31T17:18:22Z")

</div>

I havent played with polymorphic associations yet, but is there some  
reason  
this wont work?

class Comment \<ActiveRecord::Base  
belongs\_to :commentable, :polymorphic =\> true  
belongs\_to :user  
end

> That’s the problem with rails’ strict naming conventions… if you  
> want to create more than one link between the same two tables, you’re  
> a bit stuck. Hope this helps.

That’s a common misconception. The naming is just a convention (or, an  
easy  
default)-- it can always be configured differently.

class Comment \<ActiveRecord::Base  
belongs\_to :commentable, :polymorphic =\> true  
belongs\_to :author, :class\_name =\> “User”, :foreign\_key =\> “user\_id”  
end

Would work the same as the above.

Maurice
