Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Rails 4:what';这个范围怎么了?_Ruby_Ruby On Rails 4_Has And Belongs To Many - Fatal编程技术网

Ruby Rails 4:what';这个范围怎么了?

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。这可能是所有这一切的原因吗?不确定品牌范围试图做什

在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。这可能是所有这一切的原因吗?

不确定品牌范围试图做什么,但假设是找到具有品牌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)}