I have 3 tables, news,activities, threads. threads contains all the replies for news and activities. Therefore threads has a category_id and postid column to map a reply to a table and initial thread. How do i say that news has many threads, but threads has a generic post_id foreign key field, not a news_id foreign key field as well as a category_id of 1 (or 2 if its activities)? thanks adam
Hi Adam, I''ll take a guess that you want to do this: -news id serial body_text text -activities id serial body_text text -replies id serial owner_class varchar(255) owner_id int body_text text Class News < ActiveRecord::Base has_many :replies, :foreign_key => "owner_id" :conditions => "owner_class = ''News''" :order => ''id'' end Class Activities < ActiveRecord::Base has_many :replies, :foreign_key => "owner_id" :conditions => "owner_class = ''Activities''" :order => ''id'' end Class Replies < ActiveRecord::Base end I am doing exactly this for images in store.rb project. If someone has a better idea I''d like to hear it too! Peter _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
why not just use single table inheritance? posts ---------- id type (magic column name) body replies ---------- id post_id body class Post < ActiveRecord::Base end class NewsPost < Post has_many :replies end class Article < Post has_many :replies end class Reply < ActiveRecord::Base belongs_to :post end news_post = NewsPost.create("foobar") news_post.replies << Reply.create("barfoo") post = Post.find(:first) # post == NewsPost object On 11/20/05, Peter Michaux <petermichaux-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi Adam, > > I''ll take a guess that you want to do this: > > -news > id serial > body_text text > > -activities > id serial > body_text text > > -replies > id serial > owner_class varchar(255) > owner_id int > body_text text > > > > Class News < ActiveRecord::Base > has_many :replies, > :foreign_key => "owner_id" > :conditions => "owner_class = ''News''" > :order => ''id'' > end > > Class Activities < ActiveRecord::Base > has_many :replies, > :foreign_key => "owner_id" > :conditions => "owner_class = ''Activities''" > :order => ''id'' > end > > Class Replies < ActiveRecord::Base > end > > > I am doing exactly this for images in store.rb project. If someone has a > better idea I''d like to hear it too! > > Peter > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Could this all be in just one table since posts and replies are so similar? I haven''t used single table inheritance before but maybe something like this will work? posts ---------- id type (magic column name: News, Article, Reply) body parent_id class Post < ActiveRecord::Base acts_as_tree end class News < Post end class Article < Post end class Reply < Post end A new thread (either News or Article) will have parent_id NULL. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails