# Polymorphic associations

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations/194048>\
**Category:** Rails\
**Created:** [August 29, 2010, 1:14am UTC](https://www.ruby-forum.com/t/polymorphic-associations/194048 "2010-08-29T01:14:51Z")\
**Posts on this page:** 2\
**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, 1:14am UTC](https://www.ruby-forum.com/t/polymorphic-associations/194048/1 "2010-08-29T01:14:51Z")

</div>

Ok, I’ve been on this issue for several hours already. If someone with  
more RoR experience can help me out. It will be much appreciated.

A small description of what I am trying to do. In the deal show view.  
I want to display comments for a particular deal. So far everything  
works, except that I am unable to list down the comments. I can submit  
comments without any problems.

Here is my code:

social migration:

class CreateSocials \< ActiveRecord::Migration  
def self.up  
create\_table :socials do |t|  
t.references :user  
t.references :deal  
t.references :entity, :polymorphic =\> true  
#t.references :commentable, :polymorphic =\> true

```
  t.timestamps
end

```

end

def self.down  
drop\_table :socials  
end  
end

* * *

social model class

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

#Social table should have belongs\_to :entity, :polymorphic =\> true  
#t.references :entity, :polymorphic =\> true  
#belongs\_to :entity, :polymorphic =\> true  
end

* * *

comment migration:

class CreateComments \< ActiveRecord::Migration  
def self.up  
create\_table :comments do |t|  
#t.references :user  
#t.references :entity, :polymorphic =\> true  
#t.integer :commentable\_id  
#t.string :commentable\_type

```
  t.text :comment

  t.timestamps
end

```

end

def self.down  
drop\_table :comments  
end  
end

* * *

comment model class:

class Comment \< ActiveRecord::Base  
#belongs\_to :user  
#belongs\_to :entity, :polymorphic =\> true

#has\_one :social  
#belongs\_to :user

#def after\_create  
#why dont you make the form on Comment, and create the Social  
record in Comments after\_create hook or something like that?  
#Social.create(:entity =\> self)  
#end  
end

* * *

deal migration:

class CreateDeals \< ActiveRecord::Migration  
def self.up  
create\_table :deals do |t|  
t.references :city  
t.references :user

```
  t.string :title
  t.text :description
  t.integer :price
  t.integer :value
  t.integer :discount
  t.integer :savings
  t.integer :goal
  t.datetime :countdown
  t.integer :purchased

  t.timestamps
end

```

end

def self.down  
drop\_table :deals  
end  
end

* * *

deal model 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, :as =\> :entity  
#has\_many :comments, :through =\> :socials, :source =\> :comments  
#has\_many :socials; has\_many :comments, :through =\> :socials  
#has\_many :social, :dependent =\> :destroy  
#has\_many :socials, :as =\> :entity

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

#has\_attached\_file :photo, :styles =\> { :small =\> “150x150\>” },

# :url =\> "/assets/

deals/:id/:style/:basename.:extension",

# :path =\> ":rails\_root/public/assets/

deals/:id/:style/:basename.:extension"

#validates\_attachment\_presence :photo  
#validates\_attachment\_size :photo, :less\_than =\> 5.megabytes  
#validates\_attachment\_content\_type :photo, :content\_type =\> [‘image/  
jpeg’, ‘image/png’]

#def self.search(search, page)

# paginate :per\_page =\> 1, :page =\> page,

# #:conditions =\> [‘name like ?’, “%#{search}%”],

# :order =\> ‘created\_at’

#end

define\_index do  
indexes title, :sortable =\> true  
indexes description  
#indexes :name, :sortable =\> true  
#indexes comments.content, :as =\> :comment\_content  
#indexes [author.first\_name, author.last\_name], :as  
=\> :author\_name  
indexes [user.first\_name, user.last\_name], :as =\> :full\_name

```
#has author_id, created_at
has created_at

```

end

def comments

# self.socials.collect { |a| a.comment }

```
#Comment.find(:conditions => 'id = 1')
self.socials.collect { |a| a.comment }

```

end  
end

* * *

This is my view:  
deal show view:

… some html stuff here pertaining to the @deal object …

\<% @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 %\> |

\<% form\_for [@comment, Comment.new] do |f| %\>  
\<%= f.error\_messages %\>  
\<%= hidden\_field\_tag :deal\_id, params[:id] %\>

\<%= f.label :comment, 'New comment' %\>  
 \<%= f.text\_area :comment %\>

\<%= f.submit 'Add comment' %\>

\<% end %\>

The problem is. “:through” does not work properly with polymorphic  
associations…

Any assistance on this issue is a big help.

---

<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, 1:30am UTC](https://www.ruby-forum.com/t/polymorphic-associations/194048/2 "2010-08-29T01:30:40Z")

</div>

For those who are wondering. I need the social table class, to keep  
track of user interactions. An interaction can be comment, upload,  
like, etc. I need this table, because in the future I will have a page  
that displays recent user interactions sorted by created\_at

On Aug 29, 7:14 am, Christian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
