【发布时间】:2015-08-23 00:16:50
【问题描述】:
我使用 has_many :through 连接表创建了“多对多”关系。
class ProductType < ActiveRecord::Base
has_many :assemblies
has_many :components, through: :assemblies
end
class Assembly < ActiveRecord::Base
belongs_to :product_type
belongs_to :component
end
class Component < ActiveRecord::Base
has_many :assemblies
has_many :product_types, through: :assemblies
end
从完整的组件列表中,我可以选择一些并将它们添加到 product_type 但我应该只被允许添加一次,所以要添加的组件列表应该只显示尚未添加的组件。
我目前正在使用它 1) 已经添加的组件列表
@product_type.components
2) 尚未添加的列表
Component.all - @product_type.components
这可行,但是,我想在组件模型上创建一个范围,以便类似
@product_type.components.addable
将生成尚未添加的组件的第二个列表。
这可能吗?如果是,组件模型上的代码是什么?还是会进入 ProductType? (我还没有找到任何有效的方法) 另外,这个尚未添加的组件列表是否有名称?会是“右外连接”吗?如果不是,那是什么?
谢谢
【问题讨论】:
标签: ruby-on-rails-4 arel