I’ve created a helper which checks a current_user’s role to protect some attributes from being edited by an unauthorised user.
The validation works fine apart from when I’m in the console. Obviously there’s no user but it’s still trying to validate and I get:
NoMethodError: undefined method `has_role?' for nil:NilClass
How can I bypass this validation or is there a better way to achieve what I’m doing?
Location Model:
validate :check_archived_status, :on => :update
def check_archived_status
unless UserHelper.staff
if self.archived == false
if stuff == otherstuff
do some other stuff
end
end
end
end
UserHelper:
def self.staff
staff = User.current_user.has_role?('Super', 'Admin', 'Tech')
end
Can’t you populate
current_userwith some random user on your console?Like this:
current_user = User.last, so the validation doesn’t give you any errors.