You can wrap a models find method inside a with_scope call. That way all calls to find will be scoped. Here is an example:
class SomeClass < ActiveRecord::Base class << self def find(*args) self.with_scope(:find => {:conditions => "active>0"}) do super end end end
end
All calls to find will automatically have the "active>0" condition added.
If you access this class through an association from another class you could add a :conditions argument to the association. Here is an example:
class Menu < ActiveRecord::Base has_many :items, :conditions => 'active>0' end
class Item < ActiveRecord::Base belongs_to :menu end
@menu.items returns a list of active items. Now maybe you don't want to always block access to inactive items. You can create multiple named associations to the same class:
class Menu < ActiveRecord::Base has_many :items, has_many :active_items, :class_name => "Item", :conditions => 'active>0' end
Admin users could use the items association while everyone else goes through active_items.
Aaron