Create a gist now

Instantly share code, notes, and snippets.

Demonstration of with_scope for class methods which operate on scoped records
Notification.for_user(user).expire!
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment