【问题标题】:How to remove some of default scope in Arel如何删除 Arel 中的一些默认范围
【发布时间】:2013-01-28 17:27:47
【问题描述】:

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

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)

【问题讨论】:

    标签: ruby-on-rails-3 arel


    【解决方案1】:

    不,unscoped 不接受参数。
    看来,在您的情况下,您最好定义正常的scopes(原为:named_scopes)。

    只有当您需要default_scope 中定义的数据总是(或几乎)时,才应使用 default_scopes。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多