What''s the best (or most rails-like) way of modelling a many to many relationship between an object and a number of different types of object? IE, consider a system with pages, articles, news_items and a comments system whereby any of these assets may have comments? So a page has many comments, as do articles and news_items. Option 1 ======= Store info with each comment Comments Table -------------- id contents news_item_id page_id article_id Doesn''t seem very nice, but has the ability to call page.comments for example. Structure (wrongly, in my mind) implies that a comment could belong to more than one type of asset. Option 2 ======= Store info with lookup Comments Table -------------- id contents Lookup Table ------------ comment_id asset_id asset_type (eg... page, article, news_item) This would prob be how I would model it in previous apps. This would require helper functions written for things to make them know about the relationship though and doesn''t seem to be very rails''ish. Any other recommendations? Thanks in advance!
there are two methods Either have a comments table like this: Comments id table_name contents or a comments table like this: Comments id contents If you choose the second route, you will have to create the has_and_belongs_to_many tables for each table you''re linking comments with. If you choose the first route, you can use the has_many :comments, :conditions => ["table_name = ?", "articles"] or something like that, for each of your pages. That seems better to me since you probably aren''t going to have a comment appearing in multiple pages. -Jeff ----- Original Message ----- From: "Richard Livsey" <richard-gfRugNUWsoQdnm+yROfE0A@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Tuesday, June 28, 2005 4:23 PM Subject: [Rails] Many to many of many relationship> What''s the best (or most rails-like) way of modelling a many to many > relationship between an object and a number of different types of object? > > IE, consider a system with pages, articles, news_items and a comments > system whereby any of these assets may have comments? > > So a page has many comments, as do articles and news_items. > > Option 1 > =======> > Store info with each comment > > Comments Table > -------------- > id > contents > news_item_id > page_id > article_id > > Doesn''t seem very nice, but has the ability to call page.comments for > example. Structure (wrongly, in my mind) implies that a comment could > belong to more than one type of asset. > > Option 2 > =======> > Store info with lookup > > Comments Table > -------------- > id > contents > > Lookup Table > ------------ > comment_id > asset_id > asset_type (eg... page, article, news_item) > > This would prob be how I would model it in previous apps. This would > require helper functions written for things to make them know about the > relationship though and doesn''t seem to be very rails''ish. > > Any other recommendations? > > Thanks in advance! > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, 28 Jun 2005, Richard Livsey wrote:> What''s the best (or most rails-like) way of modelling a many to many > relationship between an object and a number of different types of > object?> IE, consider a system with pages, articles, news_items and a comments > system whereby any of these assets may have comments?Make pages, articles, news_items subclasses of Features. Join the comments to the features. Probably won''t work if their table structures aren''t all one big table. -- _Deirdre web / blog: http://deirdre.net/ yarn: http://fuzzyorange.com cat''s blog: http://fuzzyorange.com/vsd/ "Memes are a hoax! Pass it on!"
Deirdre Saoirse Moen wrote:>On Tue, 28 Jun 2005, Richard Livsey wrote: > > > >>What''s the best (or most rails-like) way of modelling a many to many >>relationship between an object and a number of different types of >>object? >> >> > > > >>IE, consider a system with pages, articles, news_items and a comments >>system whereby any of these assets may have comments? >> >> > >Make pages, articles, news_items subclasses of Features. > >Join the comments to the features. > >Probably won''t work if their table structures aren''t all one big table. > > >I considered that, as technically I guess they are subclasses of ''feature'' or something. But really the only thing in common is that you can comment on them. IE in the system there will be the ability to add comments to pretty much anything, but if modelled like this then everything would end up in one table! In the actual app it''s not the pages and comments etc... but it''s easier explained this way and the relationships are the same.
> > ----- Original Message ----- > From: "Richard Livsey" > >> What''s the best (or most rails-like) way of modelling a many to many >> relationship between an object and a number of different types of >> object? >> >> IE, consider a system with pages, articles, news_items and a comments >> system whereby any of these assets may have comments? >> >> So a page has many comments, as do articles and news_items. >Jeffrey Moss wrote:> there are two methods > > Either have a comments table like this: > > Comments > id > table_name > contents > > or a comments table like this: > > Comments > id > contents > > If you choose the second route, you will have to create the > has_and_belongs_to_many tables for each table you''re linking comments > with. > If you choose the first route, you can use the has_many :comments, > :conditions => ["table_name = ?", "articles"] or something like that, > for each of your pages. That seems better to me since you probably > aren''t going to have a comment appearing in multiple pages.Like you say, the 2nd route would imply that a comment can belong to multiple items. However, with the first option, where does the mapping of item to comment come in? Would this be stored in the comments table (as item_id or somesuch) or in a lookup table? The relationship, as I see it, is something along the lines of: Comments belongs_to :page belongs_to :article belongs_to :news_item Pages has_many :comments Articles has_many :comments
has_many :comments, :conditions => ["table_name = ?", "articles"] That will automatically append those conditions onto the comments, so when you say: obj = Article.find(1) obj.comments it will return all comments where comments.table_name = ''articles'' -Jeff ----- Original Message ----- From: "Richard Livsey" <richard-gfRugNUWsoQdnm+yROfE0A@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Tuesday, June 28, 2005 4:45 PM Subject: Re: [Rails] Many to many of many relationship> > >> ----- Original Message ----- >> From: "Richard Livsey" >> >>> What''s the best (or most rails-like) way of modelling a many to many >>> relationship between an object and a number of different types of >>> object? >>> >>> IE, consider a system with pages, articles, news_items and a comments >>> system whereby any of these assets may have comments? >>> >>> So a page has many comments, as do articles and news_items. >> > Jeffrey Moss wrote: > >> there are two methods >> >> Either have a comments table like this: >> >> Comments >> id >> table_name >> contents >> >> or a comments table like this: >> >> Comments >> id >> contents >> >> If you choose the second route, you will have to create the >> has_and_belongs_to_many tables for each table you''re linking comments >> with. >> If you choose the first route, you can use the has_many :comments, >> :conditions => ["table_name = ?", "articles"] or something like that, for >> each of your pages. That seems better to me since you probably aren''t >> going to have a comment appearing in multiple pages. > > > Like you say, the 2nd route would imply that a comment can belong to > multiple items. > However, with the first option, where does the mapping of item to comment > come in? > Would this be stored in the comments table (as item_id or somesuch) or in > a lookup table? > > The relationship, as I see it, is something along the lines of: > > Comments > belongs_to :page > belongs_to :article > belongs_to :news_item > > Pages > has_many :comments > > Articles > has_many :comments > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jeffrey Moss wrote:> has_many :comments, :conditions => ["table_name = ?", "articles"] > > That will automatically append those conditions onto the comments, so > when you say: > > obj = Article.find(1) > obj.comments > > it will return all comments where comments.table_name = ''articles'' > > -JeffBut doesn''t a has_many need the item ID in the table? So I guess (thinking out loud now) specifying the same foreign key for each would solve that... has_many :comments, :conditions => ["table_name = ?", "articles"], :foreign_key => "item_id" Then the same in the other models but with the associated table name. This way there''s just ''item_id'' which contains the ID of whatever asset it''s linked to. Thanks for the help, this might just work!
Oh. That''s what I meant. Mental block I guess. That should work. -Jeff ----- Original Message ----- From: "Richard Livsey" <richard-gfRugNUWsoQdnm+yROfE0A@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Tuesday, June 28, 2005 5:04 PM Subject: Re: [Rails] Many to many of many relationship> Jeffrey Moss wrote: > >> has_many :comments, :conditions => ["table_name = ?", "articles"] >> >> That will automatically append those conditions onto the comments, so >> when you say: >> >> obj = Article.find(1) >> obj.comments >> >> it will return all comments where comments.table_name = ''articles'' >> >> -Jeff > > But doesn''t a has_many need the item ID in the table? > So I guess (thinking out loud now) specifying the same foreign key for > each would solve that... > > has_many :comments, :conditions => ["table_name = ?", "articles"], > :foreign_key => "item_id" > > Then the same in the other models but with the associated table name. > This way there''s just ''item_id'' which contains the ID of whatever asset > it''s linked to. > > Thanks for the help, this might just work! > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails