4

假设我在表上有几个默认范围

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)
4

1 回答 1

2

不,unscoped不接受参数。
似乎在您的情况下,您最好定义正常scopes(是:) named_scopes

仅当您始终(或几乎)需要default_scope.

于 2013-01-28T19:40:20.653 回答