dipth / Heritage

A gem for using Multiple Table Inheritance with rails 3
50 stars 18 forks source link

Question: heritage and ActiveRecord associations #15

Open hagitgit opened 11 years ago

hagitgit commented 11 years ago

Hi,

Thanks for the great gem.

I couldn't figure out how to implement associations when the type of the heir is unknown. Example: I want to do something like:

class Post < ActiveRecord::Base
  acts_as_predecessor
  belongs_to :site
end

class BlogPost < ActiveRecord::Base
  acts_as_heir_of :post
end

class ImagePost < ActiveRecord::Base
  acts_as_heir_of :post
end

class Site < ActiveRecord::Base
  has_many :posts #could be BlogPost or ImagePost 

  def create_some_posts
      post1 = BlogPost.create
      posts << post1
      post2 = ImagePost.create
      posts << post2
  end
end

How should I fix this code? Please advise. Thanks!

dipth commented 10 years ago

@hagits have you tried doing polymorphic associations? http://guides.rubyonrails.org/v3.2.13/association_basics.html#polymorphic-associations