【问题标题】:warning when using a named_scope as part of an anonymous scope将 named_scope 用作匿名范围的一部分时发出警告
【发布时间】:2010-03-22 19:04:46
【问题描述】:

我有以下命名范围:

named_scope :find_all_that_match_tag, lambda { |tags| {
            :select => "articles.id, tags.name",
            :joins => :tags,
            :conditions => ["tags.name IN (?)",tags]}
          }

它在脚本/控制台中可以正常工作

Article.find_all_that_match_tag(["cooking"])

但如果我这样使用它,作为匿名范围的一部分

scope = Article.scoped({})
scope = scope.scoped.find_all_that_match_tag(["cooking"])

我在第二行收到警告:

/Users/Server/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:13: warning: multiple values for a block parameter (0 for 1)
from /Users/Server/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active_record/named_scope.rb:92

它仍然有效,但导致警告的原因是什么?请问我该如何摆脱它?

【问题讨论】:

    标签: ruby-on-rails activerecord warnings named-scope anonymous-scope


    【解决方案1】:

    首先,我可能不会费心在没有条件的情况下包含匿名范围。

    也就是说,我认为警告是在对 scoped 的调用中作为链的一部分而没有参数。没有必要,您有一​​个命名范围“find_all_that_match”,您应该能够简单地链接到任何以前的范围,匿名或命名。

    scope = Article.scoped({})
    scope.find_all_that_match_tag(["cooking"])
    

    也可能值得使用更短的命名范围,如“tagged_as”或简单的“tagged”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2018-02-08
      • 2010-10-22
      相关资源
      最近更新 更多