Reputation: 336
A 'user
has many posts
and a product
has many posts
, and any given post
can belong to either a user
or a product
but not both.
I think a has_many :through
relationship stored in a posts_relationships
table and written like:
Class User < ActiveRecord::Base
has_many :posts, :through => posts_relationships
and
Class Product < ActiveRecord::Base
has_many :posts, :through => posts_relationships
would express what I need. Is that the correct and simplest way to do it? It's not a complex relationship so I want to write it as simply as possible.
Upvotes: 0
Views: 116
Reputation: 56
consider polymorphic association.
Class User < ActiveRecord::Base
has_many :posts, :as=>:postings
end
Class Product < ActiveRecord::Base
has_many :posts, :as=>:postings
end
class Post
belongs_to :posting, :polymorphic=> :true
end
Upvotes: 2