Skip to content

Instantly share code, notes, and snippets.

@brandonbloom
Created August 26, 2011 08:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonbloom/1172981 to your computer and use it in GitHub Desktop.
Save brandonbloom/1172981 to your computer and use it in GitHub Desktop.
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