使用named_scope作为匿名范围的一部分时发出警告
我有以下命名范围:
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
它仍然有效,但是什么导致了警告?请问我该如何摆脱它?
I have the following named scope:
named_scope :find_all_that_match_tag, lambda { |tags| {
:select => "articles.id, tags.name",
:joins => :tags,
:conditions => ["tags.name IN (?)",tags]}
}
It works fine like this in script/console
Article.find_all_that_match_tag(["cooking"])
But if i use it like this, as part of an anonymous scope
scope = Article.scoped({})
scope = scope.scoped.find_all_that_match_tag(["cooking"])
i get a warning, on the second line:
/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
It still works, but whats causing the warning? and how do i get rid of it please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我可能不会无条件地包含一个匿名范围。
也就是说,我认为警告是在对范围作为链的一部分的调用中没有任何参数。这不应该是必要的,您有一个命名范围“find_all_that_match”,您应该能够简单地链接到任何以前的范围(匿名或命名)。
可能还值得使用较短的命名范围,例如“tagged_as”或简单的“tagged”
First of all I would probably not bother including an anonymous scope without conditions.
That said, I think the warning is in the call to scoped as part of the chain with no argument. It shouldn't be necessary, you have a named scope "find_all_that_match" that you should be able to simply chain to any previous scopes, anonymous or named.
Might also be worth using a shorter named scope like "tagged_as" or simply "tagged"