我有以下命名范围:

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

这仍然有效,但是什么导致警告?我如何摆脱它吗?

有帮助吗?

解决方案

所有我可能不会打扰包括无条件匿名范围的初

也就是说,我认为该警告是在调用作用域确定为不带参数的链的一部分。它不应该是必要的,你有一个名为范围“find_all_that_match”,你应该能够简单地连锁以往任何范围,匿名或命名。

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top