【发布时间】:2016-10-12 03:49:52
【问题描述】:
如何在rails4.1.9中替换这个查询
AuditArea.send(query_options[:include_retired] ? :with_exclusive_scope : :with_scope) {
# some stuff
}
获取错误未定义方法`with_scope'。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 ruby-2.2
如何在rails4.1.9中替换这个查询
AuditArea.send(query_options[:include_retired] ? :with_exclusive_scope : :with_scope) {
# some stuff
}
获取错误未定义方法`with_scope'。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 ruby-2.2
在较新的 Rails 版本中,with_scope 现在称为 scoping。 with_exclusive_scope 现在应该是 unscoped。这两种方法都接受一个块,因此您的代码应该可以正常使用它们。
有关详细信息,请参阅 scoping 和 unscoped 的文档。
更新:scoping 方法在类本身上调用时不起作用。它必须已经在作用域上调用(而不是在裸模型类上工作的unscoped)。我首先将“无害”范围 all(它选择所有记录,因此与裸模型类 AuditArea 的行为方式相同)添加到选择中,以便 send 的两个变体都可以工作:
AuditArea.all.send(query_options[:include_retired] ? :unscoped : :scoping) {
# ...
}
【讨论】:
scoping 是 Rails 4.1.9 中的defined method。