Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 663 Bytes

README.markdown

File metadata and controls

26 lines (20 loc) · 663 Bytes

Objective

demo how to use polymorphic with has_many through at both side

Environment

rails 4.0.0
ruby 1.9.3

Core code relation

class Article < ActiveRecord::Base
  has_many :attachable_entities, as: :entity
  has_many :attachables, through: :attachable_entities, source: :attachable, source_type: Image 
end

class AttachableEntity < ActiveRecord::Base
  belongs_to :entity, polymorphic: true
  belongs_to :attachable, polymorphic: true
end

class Image < ActiveRecord::Base
  has_many :attachable_entities, as: :attachable
  has_many :entities, through: :attachable_entities, source: :entity, source_type: Article 
end