allenhwkim
allenhwkim

Reputation: 27738

How to remove some of default scope in Arel

Let's say that I have several default scoping on table

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)

and when I want to find users with regardless of first_name , only I can do is unscope it and redefining the default scope again

User.unscoped.where(:deleted_at=>nil).where(:last_name=>"Kim")

Is there a way to unscope certain keys like the following?

User.unscoped(:first_name)

Upvotes: 4

Views: 1965

Answers (1)

kr1
kr1

Reputation: 7485

No, unscoped does not accept parameters.
it seems that in your case you would be better off defining normal scopes (was: named_scopes).

you should use default_scopes only if you need the data always (or almost) as defined in the default_scope.

Upvotes: 2

Related Questions