代码之家  ›  专栏  ›  技术社区  ›  dangerousdave

将命名的\作用域用作匿名作用域的一部分时发出警告

  •  0
  • dangerousdave  · 技术社区  · 16 年前

    我有以下命名范围:

    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
    

    它仍然有效,但是什么导致了警告?我该怎么处理呢?

    1 回复  |  直到 16 年前
        1
  •  1
  •   mczepiel    16 年前

    首先,我可能不会费心在没有条件的情况下包括匿名作用域。

    也就是说,我认为警告是作为链的一部分调用scoped的,没有任何参数。不需要这样做,您有一个命名的作用域“查找所有匹配的”您应该能够简单地链接到任何以前的作用域,匿名的或命名的。

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

    可能还值得使用一个较短的命名范围,比如“tagged_as”或“tagged”