Jer*_* F. 11 scopes ruby-on-rails-3 ruby-on-rails-3.1
我偶然发现了一篇关于Rails 3+范围的精彩文章:http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly -named-范围/ index.html中
你可以在那里阅读(在'Crazy Town'部分),可以合并来自不同模型的范围,如下所示:
class User < ActiveRecord::Base
scope :published, lambda {
joins(:posts).group("users.id") & Post.published
}
end
Run Code Online (Sandbox Code Playgroud)
它按预期工作,并允许您这样做:
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
Run Code Online (Sandbox Code Playgroud)
我在我的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>'
Run Code Online (Sandbox Code Playgroud)
==>不再工作了.
这让我感到难过,因为范围合并很棒,现在我不能像我想的那样干.
你知道吗 :
Ben*_*ret 17
该&方法看起来不再有效(太糟糕了,我发现语法很整洁).您可以将其替换为ActiveRecord::Relation#merge:
class User < ActiveRecord::Base
scope :published, lambda {
joins(:posts).group("users.id").merge(Post.published)
}
end
Run Code Online (Sandbox Code Playgroud)
编辑
看起来它不会回来,在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.
Run Code Online (Sandbox Code Playgroud)
这是提交弃用它,以防有人感兴趣:https://github.com/rails/rails/commit/66003f596452aba927312c4218dfc8d408166d54
| 归档时间: |
|
| 查看次数: |
2837 次 |
| 最近记录: |