Joshua Muheim
2006-Aug-16 15:13 UTC
[Rails] Creating a comments system for multiple types of content
Hi all My site has news entries, it has a party calendar and it has a blog. Now I''d like to create a commenting system that in fact is quite simple, but I wanna use it for all these 3 types of content. My question: how can I do that? I maybe could add an association to all three types in the comment model: class Comment < ActiveRecord::Base belongs_to :news_item belongs_to :party belongs_to :blog_entry end And in all the three models I then added a "has_many :comments" statement. This would work, but every comment entry would belong only to one of the three models, so there were many NULL values that seem incorrect to me. And when another content type would be created (like party_report) I would have to update the comment model and it would get more and more xxx_id fields and NULL values. Do you see a better way for doing this? I''m still looking for a "clean" way, but I can''t really find one using the Rails stuff I know. My idea is something like having another attribute in the comment model that specifies the sort of content (model) it is associated with: create_table :comments do |t| t.column "subject", :string, :null => false t.column "body", :text, :null => false t.column "model_type_id", :integer, :null => false t.column "model_type", :varchar, :null => false end Then the data rows could look like these: # This item is associated with news_item ID = 1 some_comment: subject: "this is a comment" body: "and this is its content" model_type_id: 1 model_type: "news_item" # This item is associated with news_item ID = 2 another_comment: subject: "this is another comment" body: "and this is another content" model_type_id: 2 model_type: "news_item" # This item is associated with party ID = 5 yet_another_comment: subject: "this is yet another comment" body: "and this is yet another content" model_type_id: 5 model_type: "party" # This item is associated with blog ID = 99 blog_comment: subject: "this is a blog comment" body: "and this is a blog comment content" model_type_id: 99 model_type: "blog_entry" Do you see what I mean? Wouldn''t this be beautiful? If I wanted to add the party_report to it I only had to add the statement "belongs_to :party_report" to the comment model and "has_many :comments" to the party_report model and then I could begin adding comments without having to change anything in the database table! # This item is associated with party_report ID = 421 blog_comment: subject: "yeah great party" body: "blah blah blah" model_type_id: 421 model_type: "party_report" Is there some way to achieve this? This would be REALLY GREAT! And Rails is called "great", so I''m looking forward! ;-) Greetings, Josh -- Posted via ruby-forum.com.
heri rakotomalala
2006-Aug-16 22:24 UTC
[Rails] Creating a comments system for multiple types of content
hi that is what i use for my website sprinj.com i also use (for example) class People < ActiveRecord::Base has_many :comments, :conditions => [''kind = ?'', ''people''], :include => :user, :order => ''created_at DESC'' ...other stuff... end the :include => :user stuff gets the user who wrote the comment i write the same stuff for other kinds of models it feels a bit clumsy for me, because the thing is you can''t go the other way. for a comment, i would like to get the item commented like for example comment.commented_item but i haven''t found a way. On 8/16/06, Joshua Muheim <forum@josh.ch> wrote:> > Hi all > > My site has news entries, it has a party calendar and it has a blog. > Now I''d like to create a commenting system that in fact is quite simple, > but I wanna use it for all these 3 types of content. > My question: how can I do that? I maybe could add an association to all > three types in the comment model: > > class Comment < ActiveRecord::Base > belongs_to :news_item > belongs_to :party > belongs_to :blog_entry > end > > And in all the three models I then added a "has_many :comments" > statement. This would work, but every comment entry would belong only to > one of the three models, so there were many NULL values that seem > incorrect to me. > And when another content type would be created (like party_report) I > would have to update the comment model and it would get more and more > xxx_id fields and NULL values. > > Do you see a better way for doing this? I''m still looking for a "clean" > way, but I can''t really find one using the Rails stuff I know. > > My idea is something like having another attribute in the comment model > that specifies the sort of content (model) it is associated with: > > create_table :comments do |t| > t.column "subject", :string, :null => false > t.column "body", :text, :null => false > t.column "model_type_id", :integer, :null => false > t.column "model_type", :varchar, :null => false > end > > Then the data rows could look like these: > > # This item is associated with news_item ID = 1 > some_comment: > subject: "this is a comment" > body: "and this is its content" > model_type_id: 1 > model_type: "news_item" > > # This item is associated with news_item ID = 2 > another_comment: > subject: "this is another comment" > body: "and this is another content" > model_type_id: 2 > model_type: "news_item" > > # This item is associated with party ID = 5 > yet_another_comment: > subject: "this is yet another comment" > body: "and this is yet another content" > model_type_id: 5 > model_type: "party" > > # This item is associated with blog ID = 99 > blog_comment: > subject: "this is a blog comment" > body: "and this is a blog comment content" > model_type_id: 99 > model_type: "blog_entry" > > Do you see what I mean? Wouldn''t this be beautiful? If I wanted to add > the party_report to it I only had to add the statement "belongs_to > :party_report" to the comment model and "has_many :comments" to the > party_report model and then I could begin adding comments without having > to change anything in the database table! > > # This item is associated with party_report ID = 421 > blog_comment: > subject: "yeah great party" > body: "blah blah blah" > model_type_id: 421 > model_type: "party_report" > > Is there some way to achieve this? This would be REALLY GREAT! And Rails > is called "great", so I''m looking forward! ;-) > > Greetings, > Josh > > -- > Posted via ruby-forum.com. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > lists.rubyonrails.org/mailman/listinfo/rails >-- ______________ Heri R. sprinj.com -------------- next part -------------- An HTML attachment was scrubbed... URL: wrath.rubyonrails.org/pipermail/rails/attachments/20060816/ab7c3fe5/attachment.html