警告

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」或簡稱「標記」