假设我在表上有几个默认的范围
class User < ActiveRecord::Base
default_scope where(:first_name => 'Allen') # I know it's not realistic
default_scope where(:last_name => 'Kim') # I know it's not realistic
default_scope where(:deleted_at => nil)
end
>> User.all
User Load (0.8ms) SELECT `users`.* FROM `users`
WHERE `users`.`first_name` = 'Allen'
AND `users`.`last_name` = 'Kim' AND (`users`.`deleted_at` IS NUL
L)
当我想找到具有 first_name 的用户时,我唯一能做的就是取消它的作用域并再次重新定义默认范围
User.unscoped.where(:deleted_at=>nil).where(:last_name=>"Kim")
有没有办法取消某些键的作用域,如下所示?
User.unscoped(:first_name)
不,unscoped
不接受参数。
似乎在您的情况下,您最好定义正常scopes
(was:named_scopes
)。
仅当您始终(或几乎)需要default_scope
中定义的数据时,才应使用default_scopes。