Polymorphic has_and_belongs_to_many

随声附和 提交于 2020-05-30 07:38:28

问题


How define a has_and_belongs_to_many polymorphic association?

Situation:

Figure that we have users, tracks, lists, etc... and all these models can be tagged and use this tag for filter.

What i'm trying to do is:

Use has_and_belongs_to_many that enable to a tag to have other objects, and that other objects also can have other tags.

So for enable a tag that belongs to more than one kind of object (users or tracks or tracklists), we need to use polymorphic relations.

That's my code on this moment:

tags migration

class CreateTags < ActiveRecord::Migration[5.2]
  def change
    create_table :tags do |t|
      t.string :name
      t.references :taggable, polymorphic: true, index: true
      t.timestamps
    end
    create_table :assemblies_parts, id: false do |t|
      t.belongs_to :assembly, index: true
      t.belongs_to :part, index: true
    end
  end
end

tag model

class Tag < ApplicationRecord
  has_and_belongs_to_many :taggable, polymorphic: true
end

user model

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  has_and_belongs_to_many :tags, as: :taggable
  has_one :top_list
  has_many :tracks, through: :top_list

end

track model

class Track < ApplicationRecord
  has_and_belongs_to_many :tags, as: :taggable
  belongs_to :top_list
end

回答1:


You can use a has_many association:

Tag model

class Tag < ApplicationRecord
  has_many :relation
end

Relation model

class Relation < ApplicationRecord
  belongs_to :tag
  belongs_to :taggable, polymorphic: true
end

User model

class User < ApplicationRecord
  has_many :relations, as: :taggable
  has_many :tags, through: :relations

  has_one :top_list
  has_many :tracks, through: :top_list
end

Track model

class Track < ApplicationRecord
  has_many :relations, as: :taggable
  has_many :tags, through: :relations

  belongs_to :top_list
end

Tags migration

class CreateTags < ActiveRecord::Migration[5.2]
  def change
    create_table :tags do |t|
      t.string :name
      t.timestamps
    end
    create_table :relations, id: false do |t|
      t.references :tags, index: true
      t.references :taggable, polymorphic: true, index: true
    end
  end
end


来源:https://stackoverflow.com/questions/51564463/polymorphic-has-and-belongs-to-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!