Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails has_many:多态通过has_和_属于_many_Ruby On Rails_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails has_many:多态通过has_和_属于_many

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

如何在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_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')