警告

0

我有以下的命名范围:警告

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

首先,我可能不打扰包括没有条件的匿名范围。

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

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

也可能是值得使用较短的命名范围,如“tagged_as”或简称“标记”