class Video < ActiveRecord::Base # Image class is similar
has_many :affixtures, as: :affixable
with_options through: :affixtures, source: :affix_owner do |assn|
assn.has_many :articles, source_type: 'Article'
assn.has_many :products, source_type: 'Product'
end
# If you want to get users
# with_options source: :user do |assn|
# assn.has_many :articles_users, through: :articles
# assn.has_many :products_users, through: :products
# end
# def users
# articles_users + products_users
# end
# and so on...
end
class Affixture < ActiveRecord::Base
with_options polymorphic: true do |assn|
assn.belongs_to :affix_owner
assn.belongs_to :affix
end
end
# def change
# create_table :affixture do |t|
# t.references :affix_owner, polymorphic: true #, index: true in Rails 4
# t.references :affixable, polymorphic: true #, index: true in Rails 4
# t.timestamps
# end
# add_index :affixture, [:affix_owner_id, :affix_owner_type]
# add_index :affixture, [:affixable_id, :affixable_type]
# end
class Article < ActiveRecord::Base # Product class is similar
belongs_to :user
has_many :affixtures, as: :affix_owner
with_options through: :affixtures, source: :affixable do |assn|
assn.has_many :videos, source_type: 'Video'
assn.has_many :images, source_type: 'Image'
end
end