# Polymorphic help

**URL:** <https://www.ruby-forum.com/t/polymorphic-help/194063>\
**Category:** Rails\
**Created:** [August 29, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/polymorphic-help/194063 "2010-08-29T18:14:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [August 29, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/polymorphic-help/194063/1 "2010-08-29T18:14:59Z")

</div>

With the help of some devs. I was able to list down commends of a  
specific deal using the “def comments” method in the deal model  
class.

However, what I also need is to display the owner/user who made the  
comment via the @deal.comments loop. For example, something like this  
would be ideal:

\<% @deal.comments.each do |comment| %\>  
\<%=h comment.comment %\>  
\<%=h comment.user.first\_name %\>  
\<% end %\>

Here is my actual code so far. I believe the key is to edit the “def  
comments” method in the deal model class to include the user model or  
something like that. Any help is appreciated…

* * *

deal show view

\<% @deal.comments.each do |comment| %\>

  
  
  
  
  
  
\<% end %\>

| Comments |
| --- |
| \<%=h comment.comment %\> | \<%= link\_to ‘Show’, comment %\> | \<%= link\_to ‘Edit’, edit\_comment\_path(comment) %\> | \<%= link\_to ‘Destroy’, comment, :confirm =\> ‘Are you  
sure?’, :method =\> :delete %\> |

* * *

model deal class

class Deal \< ActiveRecord::Base  
validates\_presence\_of :city\_id

validates\_presence\_of :title, :description, :price, :value, :discount,  
:savings, :goal, :countdown, :purchased  
validates\_length\_of :title, :minimum =\> 5

belongs\_to :city  
belongs\_to :user

has\_many :features, :dependent =\> :destroy  
has\_many :photos, :dependent =\> :destroy

has\_many :socials

accepts\_nested\_attributes\_for :features, :reject\_if =\> lambda { |a|  
a[:description].blank? }, :allow\_destroy =\> true  
accepts\_nested\_attributes\_for :photos, :reject\_if =\> lambda { |a|  
a[:photo].blank? }, :allow\_destroy =\> true

define\_index do  
indexes title, :sortable =\> true  
indexes description

```
has created_at

```

end

# Polymorphic :through

# [has\_many :through - The other side of polymorphic :through associations](http://blog.hasmanythrough.com/2006/4/3/polymorphic-through)

def comments  
self.socials.collect { |social|  
social.entity if social.entity\_type == “Comment”  
}  
end  
end

* * *

social class model

class Social \< ActiveRecord::Base  
belongs\_to :deal  
belongs\_to :user  
belongs\_to :entity, :polymorphic =\> true  
has\_many :comments

end
