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 ...
<table>
<tr>
<th>Comments</th>
</tr>
<% @deal.comments.each do |comment| %>
<tr>
<td><%=h comment.comment %></td>
<td><%= link_to ''Show'', comment %></td>
<td><%= link_to ''Edit'',
edit_comment_path(comment) %></td>
<td><%= link_to ''Destroy'', comment, :confirm
=> ''Are you
sure?'', :method => :delete %></td>
</tr>
<% end %>
</table>
<% form_for [@comment, Comment.new] do |f| %>
<%= f.error_messages %>
<%= hidden_field_tag :deal_id, params[:id] %>
<p>
<%= f.label :comment, ''New comment'' %><br />
<%= f.text_area :comment %>
</p>
<p><%= f.submit ''Add comment'' %></p>
<% end %>
The problem is. ":through" does not work properly with polymorphic
associations...
Any assistance on this issue is a big help.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 ... > > <table> > <tr> > <th>Comments</th> > </tr> > > <% @deal.comments.each do |comment| %> > <tr> > <td><%=h comment.comment %></td> > <td><%= link_to ''Show'', comment %></td> > <td><%= link_to ''Edit'', edit_comment_path(comment) %></td> > <td><%= link_to ''Destroy'', comment, :confirm => ''Are you > sure?'', :method => :delete %></td> > </tr> > <% end %> > </table> > > <% form_for [@comment, Comment.new] do |f| %> > <%= f.error_messages %> > <%= hidden_field_tag :deal_id, params[:id] %> > <p> > <%= f.label :comment, ''New comment'' %><br /> > <%= f.text_area :comment %> > </p> > <p><%= f.submit ''Add comment'' %></p> > <% end %> > > The problem is. ":through" does not work properly with polymorphic > associations... > > Any assistance on this issue is a big help.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi,
I''m following the Polymorphic Associations test, issue as following.
*****
class Product < ActiveRecord::Base
has_many :images, :as=>:imageable
end
-------
class User < ActiveRecord::Base
has_many :images, :as=>:imageable
end
-------
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
**migration:
class CreateImages < ActiveRecord::Migration
def self.up
create_table :images do |t|
t.integer :imageable_id
t.string :imageable_type
t.string :name
t.timestamps
end
end
def self.down
drop_table :images
end
end
**rending views/images/show.html.erb
<p>
<b>OwnerName:</b>
<%= @image.imageable%>
</p>
*****
when i rendering the http://localhost:3000/images/1, the wrong message
as following:
*****
NameError in Images#show
Showing /home/orient/rails-app/temp1/app/views/images/show.html.erb
where line #20 raised:
wrong constant name user
Extracted source (around line #20):
17:
18: <p>
19: <b>OwnerName:</b>
20: <%= @image.imageable%>
21:
22: </p>
23:
*********
Any one could help , thanks in advance!!
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 24, 10:47 pm, Orient Fang <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > I''m following the Polymorphic Associations test, issue as following. > > **rending views/images/show.html.erb > > <p> > <b>OwnerName:</b> > <%= @image.imageable%> > > </p> > *****I think @image.imageable returns an ActiveRecord Object, which you can''t just display. If you are just testing: @image.imageable.inspect should dump the active record object the image is related to. If you are trying to get an attribute of the imageable relation, since there can be different relations you have to use the imageable_type to make sure it is there and use something like @image.imageble.name -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
But does the image an imageable? how are you saving the image? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 27, 10:41 am, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> But does the image an imageable? how are you saving the image?I was just talking about polymorphic in general. For images I''d use something like paperclip. http://github.com/thoughtbot/paperclip Then you''d have something like class Image < ActiveRecord::Base include Paperclip belongs_to :imageable, :polymorphic => true has_attached_file :photo, :styles => { :thumb=> "100x100", :small => "200x200>", :medium => "300x300>", :large => "400x400>" } end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.