在将两个模型连接到n..n关联时遇到问题。
我在上下文中具有以下关联:
与此类似,我有:
嗯,联接模型在两种情况下都具有相同的属性,但每种情况下的部分属性都不同。
是否有一种方法可以创建单个联接模型(Item),该模型可以存储所有数据,并且可以与Order和Proposal中的三个不同别名一起使用?
我试图这样编码,但是没有用。.
proposal.rb
class Proposal < ActiveRecord::Base
...
has_many :items
has_many :products, through: :items, source: :requisition, source_type: "Produto"
has_many :projects, through: :items, source: :requisition, source_type: "Projeto"
has_many :projects, through: :items, source: :requisition, source_type: "Novo"
...
end
order.rb
class Order < ActiveRecord::Base
...
has_many :items, as: :requisition, dependent: :destroy
has_many :products, through: :items, source: :requisition, source_type: "Produtos"
has_many :projects, through: :items, source: :requisition, source_type: "Projetos"
has_many :projects, through: :items, source: :requisition, source_type: "Novos"
...
end
product.rb
class Product < ActiveRecord::Base
has_many :items, as: :piece, dependent: :destroy
has_many :orders, through: :items, source: :piece, source_type: 'Order'
has_many :proposals, through: :items, source: :piece, source_type: 'Proposal'
...
end
project.rb
class Project < ActiveRecord::Base
has_many :items, as: :piece, dependent: :destroy
has_many :orders, through: :items, source: :piece, source_type: 'Order'
has_many :proposals, through: :items, source: :piece, source_type: 'Proposal'
...
end
item.rb
class Item < ApplicationRecord
belongs_to :piece, polymorphic: true
belongs_to :requisition, polymorphic: true
...
end
我希望统一这种关联,清除一些表,并尝试加快应用程序的速度。