0

在新版本的活动记录(我猜是 4.x)中,关联的条件语法如下

has_many :items, conditions: { marker:true }

已弃用。它应该看起来像

has_many :items, -> { where marked:true }

新语法看起来更复杂(在我看来),但它变得不可能与Object#with_options方法一起使用。

在旧版本中,我的代码就像

with_options conditions:{marker:true} do |assoc|
  assoc.has_many :items
end

但现在看来我不能再这样做了。

有谁知道有什么方法可以为我的所有关联添加条件,而无需一次又一次地复制/粘贴?

谢谢

4

1 回答 1

0

经过一番挖掘,当最后一个变量是 proc 对象时,调用 with_options 产生的块变量似乎有不同的响应。一种解决方法是使用空哈希结束调用。

with_options conditions:{marker:true} do |assoc|
  assoc.has_many :items, -> { where marked:true }, {}
end
于 2013-11-06T17:18:57.617 回答