A slightly more advanced twist on associations is the polymorphic association. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared:
class Picture < ActiveRecord::Base belongs_to :imageable, polymorphic: trueendclass Employee < ActiveRecord::Base has_many :pictures, as: :imageableendclass Product < ActiveRecord::Base has_many :pictures, as: :imageableend |
No comments:
Post a Comment