Ruby Rails 4:what';这个范围怎么了?
在Rails 3.2中,这样做很好:Ruby Rails 4:what';这个范围怎么了?,ruby,ruby-on-rails-4,has-and-belongs-to-many,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) } 这里有什么问题 更新 我还没有切换到强参数,我正在使用protected\u属性gem。这可能是所有这一切的原因吗?不确定品牌范围试图做什
class Component < ActiveRecord::Base
has_and_belongs_to_many :brands
...
scope :branded, ->(b) { includes(:brands).where('brands.id in (?)', b.id) }
这里有什么问题
更新
我还没有切换到强参数,我正在使用
protected\u属性
gem。这可能是所有这一切的原因吗?不确定品牌范围试图做什么,但假设是找到具有品牌X的组件,Component.branded(brand.find(X))
也许可以试试这个:
scope :branded, ->(b) { joins(:brands).where(Brand.arel_table[:id].eq b.id)}
错误就发生在那条线上?为什么要在中对单个
id
执行?只要做where(“brands.id=?”,b.id)
是的,不知道我为什么要这么做。但该行仍然存在问题。请发布您的有许多和属于组件和品牌的声明。此外,请检查您是否运行了所有挂起的迁移。您可以将完整的错误堆栈与模型文件一起发布吗?因此,这似乎可以修复它,但我在另一个has\u和\u beliens\u to \u many
关系中遇到了类似的错误。这是公共Rails 4的问题吗?不确定这是否对每个人都是常见的,但我知道在Rails 4.0之后,我将所有查询转换为arel_表,该表将在查询中使用字符串。非常有用,因此您不必每次都调用arel_table
,您只需使用品牌[:id]或组件[:title]等。您很想知道为什么这是必要的,但它现在正在工作。谢谢你的帮助!
Unknown column 'components.brand_ids' in 'where clause'
scope :branded, ->(b) { joins(:brands).where(Brand.arel_table[:id].eq b.id)}