Skip to content

Instantly share code, notes, and snippets.

@thadd
Created November 26, 2009 17:17
Show Gist options
  • Save thadd/243580 to your computer and use it in GitHub Desktop.
Save thadd/243580 to your computer and use it in GitHub Desktop.
How to scope all of a model's finders based on a user preference
class ApplicationController < ActionController::Base
around_filter :check_for_prefers_recent
protected
def check_for_prefers_recent
if current_user && current_user.show_recent_only?
Gift.scope_to_recent do
yield
end
else
yield
end
end
end
class Gift < ActiveRecord::Base
def self.scope_to_recent
with_scope(:find => {:conditions => ['gifts.updated_at > ?', 6.months.ago]}) do
yield
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment