我想知道,在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