Reputation: 396
i am using default scope in my model
def self.default_scope
where ('locked_at IS NULL' )
end
so that no one can search for locked user , but i want if the logged-in user is admin , then he should able to find out locked user , i am not sure how it will work
in the database , in user table i have column name "admin" , which tell us who is admin .
i tried for session in model , but i think this is not a good idea , please suggest me something else
Upvotes: 0
Views: 272
Reputation: 10251
You can use unscoped
it return a scope for this class without taking into account the default_scope
Something like this:
class User < ActiveRecord::Base
def self.default_scope
where ('locked_at IS NULL')
end
end
=> User.all # Fires "SELECT * FROM users WHERE locked_at = NULL"
=> User.unscoped.all if current_user.admin# Fires "SELECT * FROM users"
Don't forget to put the if
condition after unscoped. Because as you mentioned that user table has admin
column and I assume it has boolean type. If it's true
then only it should unscoped.
Upvotes: 1
Reputation: 51151
First of all, you shouldn't place space between method and brackets with its arguments:
where('locked_at is NULL')
Second. If your admin is logged in, you should probably simply use unscoped
:
User.unscoped
to avoid using your default scope.
Upvotes: 2