I have a User model. I can check whether a User is an admin by doing a_user.try(:admin?)
.
I'd like to define a named scope that gets all Users updated within the last X minutes who are not admins. So far I have:
scope :recent, lambda { { :conditions => ['updated_at > ?', 5.minutes.ago] } }
This successfully gets all Users updated within the last 5 minutes, but how do I incorporate the admin check? I don't know how to call try()
on an instance of a User inside the scope...
Instead of using
lambda
, I find it cleaner to use class methods.if admin column in users table is a boolean,
Just another possibility, usable in Rails 4,
This is just another possible format and taking in account the possibility of using Rolify gem.