通过acts_as_taggable_on rails链接模型

时间:2016-05-10 06:59:15

标签: ruby-on-rails-4 acts-as-taggable-on

我的模型Product has_many :taxons, through: :classifications。如何实现acts_as_taggable_on以使用分类名称而不是标记表中的标记来标记产品?有可能吗?

#app/models/product.rb
class Product < ActiveRecord::Base
  translates :name
  accepts_nested_attributes_for :translations, allow_destroy: true, reject_if: :all_blank

  # Associations
  has_many :classifications, inverse_of: :product, dependent: :delete_all
  has_many :taxons, through: :classifications
end

#app/models/classification.rb
class Classification < ActiveRecord::Base
  self.table_name = 'products_taxons'

  # Associations
  belongs_to :product, inverse_of: :classifications
  belongs_to :taxon, inverse_of: :classifications, touch: true
end

#app/models/taxon.rb
class Taxon < ActiveRecord::Base
  translates :name
  globalize_accessors
  has_many :classifications, inverse_of: :taxon, dependent: :delete_all
  has_many :products, through: :classifications
end

0 个答案:

没有答案