为什么在合并范围的方法上使用合并方法在Rails 3.1上不再起作用? [英] Why using merge method with scopes isn't working anymore on Rails 3.1?

查看:79
本文介绍了为什么在合并范围的方法上使用合并方法在Rails 3.1上不再起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一篇有关Rails 3+范围的精彩文章:

I stumbled upon a wonderful article about scopes on Rails 3+ : http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html

您可以在此处(在疯狂小镇"中阅读)可以合并来自不同模型的合并范围:

You can read there (in 'Crazy Town' section) that it's possible to merge scopes from different models like this :

class User < ActiveRecord::Base

  scope :published, lambda {
    joins(:posts).group("users.id") & Post.published
  }
end

它可以按预期运行,并且可以执行以下操作:

which works just as expected, and allows you to do :

User.published.to_sql
#=> SELECT users.* FROM "users"
#   INNER JOIN "posts" ON "posts"."author_id" = "users"."id"
#   WHERE (posts.published_at IS NOT NULL AND posts.published_at <= '2010-02-27 02:55:45.063181')
#   GROUP BY users.id

我在Rails 3.1项目中尝试了这种方法,显然它不再起作用了.

I tried this approach in my Rails 3.1 project and apparently it's not working anymore.

因此,我克隆了文章的Rails 3.0.0-beta1项目,从我的眼中看到,这些家伙没有撒谎,而且事情按照他们所说的进行.

So I cloned the article's Rails 3.0.0-beta1 project, saw by my eyes that the guys are not lying and things are working the way they tell.

然后我进行了3.1修改,现在我得到了:

Then I 3.1'ed it up, and now I get :

ruby-1.9.2-p290 :003 > User.published.to_sql
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."author_id" = "users"."id" GROUP BY users.id
  Post Load (0.2ms)  SELECT "posts".* FROM "posts" WHERE (posts.published_at IS NOT NULL AND posts.published_at <= '2011-10-05 11:45:00.512231')
  User Load (0.1ms)  SELECT "users".* FROM "users" 
NoMethodError: undefined method `to_sql' for []:Array
  from (irb):3
  from /home/jerefrer/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
  from /home/jerefrer/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
  from /home/jerefrer/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
  from script/rails:9:in `require'
  from script/rails:9:in `<main>'

==>不再起作用.

这让我很难过,因为合并范围非常棒,现在我不能像我想要的那样干.

And that makes me sad, because scope merging was awesome and now I can't be as DRY as I want.

你知道吗?

  • 两个版本之间发生了什么?
  • 还有其他方法可以做到吗?

推荐答案

&方法看起来不再可行(太糟糕了,我发现语法很简洁).您可以将其替换为ActiveRecord::Relation#merge:

The & method doesn't look like it works anymore (too bad, I found the syntax was neat). You can replace it with ActiveRecord::Relation#merge:

class User < ActiveRecord::Base

  scope :published, lambda {
    joins(:posts).group("users.id").merge(Post.published)
  }
end

修改

看起来不会再回来了,在rails 3.0.10中尝试它会给出不赞成使用的警告:

And it looks like it won't be back, trying it in rails 3.0.10 gives a deprecation warning:

DEPRECATION WARNING: Using & to merge relations has been deprecated and will be removed in Rails 3.1. Please use the relation's merge method, instead.

如果有人感兴趣,这里是不推荐使用的提交: https://github.com/rails/rails/commit/66003f596452aba927312c4218dfc8d408166d54

Here's the commit deprecating it, in case someone's interested: https://github.com/rails/rails/commit/66003f596452aba927312c4218dfc8d408166d54

这篇关于为什么在合并范围的方法上使用合并方法在Rails 3.1上不再起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆