n..n アソシエーションで 2 つのモデルを結合する際に問題があります。
コンテキストには次の関連付けがあります。
- 提案 n..n 製品
- 提案 n..n プロジェクト
- 提案 n..n NewProject
そして、これと同様に、私は持っています:
- 注文 n..n 製品
- オーダー n..n プロジェクト
- 注文 n..n NewProject
結合モデルは両方のケースで同じ属性を持ちますが、それぞれの部分は異なります。
すべてのデータを格納し、注文と提案の 3 つの異なるエイリアスで使用できる単一の結合モデル (アイテム) を実行する方法はありますか?
このようにコーディングしようとしましたが、うまくいきませんでした..
提案.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
この関連付けを統一し、いくつかのテーブルをクリアして、アプリを高速化しようとしています。