Hi everyone, I''m sure the answer is out there and quite simple, but I haven''t been able to find it. Suppose I have the following classes with polymorphic associations: class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Camera < ActiveRecord::Base has_many :comments, :as => :commentable end class Computer < ActiveRecord::Base has_many :comments, :as => :commentable end class Book < ActiveRecord::Base has_many :comments, :as => :commentable end How do I also make the Comment class belong_to a User class? Would the User class be parallel to the Camera, Computer, and Book classes? For example, would I define the relationship between User and Comment as such: class User < ActiveRecord::Base has_many :comments, :as => :commentable end And how would I go about referencing the comments through the User->Camera/Computer/Book association? Would it be as such?@user.computers.comments.empty? # For example Thanks in advance! Mike -- 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.
Just in case this wasn''t clear, I would like every comment to belong to a user and only be editable by that user, but be associated with a product type (Camera, Book, etc.). This is why I doubt the simple class definition I outlined below would suffice. Best, Mike On Feb 3, 4:26 pm, Mike Kim <fourcatra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi everyone, > > I''m sure the answer is out there and quite simple, but I haven''t been > able to find it. > > Suppose I have the following classes with polymorphic associations: > > class Comment < ActiveRecord::Base > belongs_to :commentable, :polymorphic => true > end > > class Camera < ActiveRecord::Base > has_many :comments, :as => :commentable > end > > class Computer < ActiveRecord::Base > has_many :comments, :as => :commentable > end > > class Book < ActiveRecord::Base > has_many :comments, :as => :commentable > end > > How do I also make the Comment class belong_to a User class? Would the > User class be parallel to the Camera, Computer, and Book classes? For > example, would I define the relationship between User and Comment as > such: > > class User < ActiveRecord::Base > has_many :comments, :as => :commentable > end > > And how would I go about referencing the comments through the User- > > >Camera/Computer/Book association? Would it be as such? > > @user.computers.comments.empty? # For example > > Thanks in advance! > > Mike-- 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.
Sounds like you could just add a "belongs_to :user" to the Comment class. But since you already have "comments" on the user, you''ll need to give the reverse association another name. Perhaps "comments_owned" So add "has_many :comments_owned, :class_name => Comment" to User. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2wZGRNY7WwAJ. 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.
Thanks, Tim Actually, and I haven''t fully tested this out yet, I think it''s simpler than that. Currently, I hvae the following: class Comment < ActiveRecord::Base belongs_to :user belongs_to :commentable, polymorphic => true ... end and that seems to be working. I have another issue now that I''ll outline in a subsequent email. Best, Mike On Feb 3, 10:28 pm, Tim Shaffer <timshaf...-BUHhN+a2lJ4@public.gmane.org> wrote:> Sounds like you could just add a "belongs_to :user" to the Comment class. > > But since you already have "comments" on the user, you''ll need to give the > reverse association another name. Perhaps "comments_owned" > > So add "has_many :comments_owned, :class_name => Comment" to User.-- 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.