为什么在Rails 3.1上使用带范围的合并方法不再有效?

时间:2011-10-05 11:53:43

标签: ruby-on-rails-3 ruby-on-rails-3.1 scopes

我偶然发现了一篇关于Rails 3+范围的精彩文章:http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html

你可以在那里阅读(在'Crazy Town'部分),可以合并来自不同模型的范围:

class User < ActiveRecord::Base

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

按预期工作,并允许您执行:

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项目中尝试了这种方法,显然它已经无法正常工作了。

所以我克隆了文章的Rails 3.0.0-beta1项目,从我的眼睛看到这些家伙没有说谎,事情按照他们说的方式运作。

然后我3.1了,现在我得到了:

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>'

==&GT;不再起作用了。

这让我感到难过,因为范围合并很棒,现在我不能像我想的那样干。

你知道吗:

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

1 个答案:

答案 0 :(得分:17)

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

class User < ActiveRecord::Base

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

修改

看起来它不会回来,在rails 3.0.10中尝试它会给出一个弃用警告:

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