Polymorphic associations

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.

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. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs