Created
August 26, 2011 08:27
-
-
Save brandonbloom/1172981 to your computer and use it in GitHub Desktop.
Demonstration of with_scope for class methods which operate on scoped records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notification.for_user(user).expire! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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