【问题标题】:moving rails2 to 4 and getting error - undefined method `with_scope'将 rails2 移动到 4 并出现错误 - 未定义的方法“with_scope”
【发布时间】: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


    【解决方案1】:

    在较新的 Rails 版本中,with_scope 现在称为 scopingwith_exclusive_scope 现在应该是 unscoped。这两种方法都接受一个块,因此您的代码应该可以正常使用它们。

    有关详细信息,请参阅 scopingunscoped 的文档。

    更新:scoping 方法在类本身上调用时不起作用。它必须已经在作用域上调用(而不是在裸模型类上工作的unscoped)。我首先将“无害”范围 all(它选择所有记录,因此与裸模型类 AuditArea 的行为方式相同)添加到选择中,以便 send 的两个变体都可以工作:

    AuditArea.all.send(query_options[:include_retired] ? :unscoped : :scoping) {
      # ...
    }
    

    【讨论】:

    • 没有作用域的作品,但作用域仍然给出错误 * NoMethodError 异常:未定义的方法 `作用域'
    • 你能粘贴完整的错误吗? scoping Rails 4.1.9 中的defined method
    • *** NoMethodError 异常:#<0x00000008174410>
    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2017-01-13
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    相关资源
    最近更新 更多