public
Created

Demonstration of with_scope for class methods which operate on scoped records

  • Download Gist
Notification.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 
class Notification < ActiveRecord::Base
 
belongs_to :user
 
scope :for_user, lambda { |user| where(:user_id => user.id) }
scope :unexpired, where('expired_at is null')
 
delf self.expire!
with_scope do
# Here we further filter by the `unexpired` scope,
# but the default scope is available as `scoped`
# and there is also an `unscoped` method.
unexpired.each &:expire!
end
end
def expire!
self.expired_at = Time.now
save!
end
 
end
gistfile1.rb
Ruby
1
Notification.for_user(user).expire!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.