Rails 3"范围"值(范围与类方法)

时间:2015-03-26 10:26:18

标签: ruby-on-rails ruby scope class-method

我想知道,在scoped关键字和类方法

的情况下,为什么scope值不同?
class A < ActiveRecord::Base
   scope :first_scope, -> { where( "1=1" ) } # to be used by both 

   scope :my_scope, -> { p "S: #{ scoped.to_sql }"; where( "2=2" ) }

   def my_scope_2
     p "S: #{ scoped.to_sql }"; 
     where( "2=2" )   
   end

end

测试打印出来的内容:

A.first_scope.my_scope     # "S: SELECT * FROM `A`"
A.first_scope.my_scope_2   # "S: SELECT * FROM `A` WHERE (1=1)

虽然它们在最后生成相同的关系对象:SELECT * FROM A WHERE (1=1) AND (2=2),但中间作用域对象对于scope定义不是(?)正确

这是预期的行为吗?

rails 3.2.21;红宝石2.1.5p273

0 个答案:

没有答案