Ruby on rails has_many:多态通过has_和_属于_many
如何在Rails 4中体现以下理念:Ruby on rails has_many:多态通过has_和_属于_many,ruby-on-rails,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby On Rails 4,Activerecord,如何在Rails 4中体现以下理念: class Package < ActiveRecord::Base has_and_belongs_to_many :categories has_many :products through :categories has_many :insurances, through :categories end class Template < ActiveRecord::Base has_and_belongs_to_man
class Package < ActiveRecord::Base
has_and_belongs_to_many :categories
has_many :products through :categories
has_many :insurances, through :categories
end
class Template < ActiveRecord::Base
has_and_belongs_to_many :categories
has_many :products through :categories
has_many :insurances, through :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :packages
has_many :products, as: :productable
has_many :insurances, as: :productable
end
class Product < ActiveRecord::Base
belongs_to productable, polymorphic: true
end
应具有可生产的\u类型='Package'和可生产的\u id:Package.id
应该有productable_type='Template'和productable_id:Template.id我相信你要找的是
这将首先保留相关记录,允许显示类型和id。我不清楚您的问题是什么。您已经编写了rails代码,并且正在询问rails 4是什么?它不是一个实际的代码,只是一个示例idea@DavidAldridge我提出了这个问题
package.categories.first.products.create(name: 'asdasd')
template.categories.first.products.create(name: 'asdasd')