使用named_scope作为匿名范围的一部分时发出警告

发布于 2024-08-26 07:20:28 字数 807 浏览 10 评论 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

它仍然有效,但是什么导致了警告?请问我该如何摆脱它?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-09-02 07:20:28

首先,我可能不会无条件地包含一个匿名范围。

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

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

可能还值得使用较短的命名范围,例如“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.

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

Might also be worth using a shorter named scope like "tagged_as" or simply "tagged"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文