Rails 4:这个范围有什么问题?

时间:2014-08-13 01:50:04

标签: ruby ruby-on-rails-4 has-and-belongs-to-many

在Rails 3.2中,这很好用:

class Component < ActiveRecord::Base
  has_and_belongs_to_many :brands
  ...
  scope :branded, ->(b) { includes(:brands).where('brands.id in (?)', b.id) }

关系的另一端:

class Brand < ActiveRecord::Base
  has_and_belongs_to_many :components

但是我升级到Rails 4并且它打破了这条消息:

Unknown column 'components.brand_ids' in 'where clause'

这里的问题是什么?

更新

我还没有切换到强参数,而且我正在使用protected_attributes宝石。这可能是造成这一切的原因吗​​?

1 个答案:

答案 0 :(得分:1)

不确定品牌范围正在尝试做什么,但假设找到具有品牌X的组件,Component.branded(Brand.find(X))

也许试试这个:

scope :branded, ->(b) { joins(:brands).where(Brand.arel_table[:id].eq b.id)}