I would like to be able to have an AR::Base descendant that only deals
with a subset of rows in a table based on some filtering criterion.
For example, let’s say I make an object “job” but I only want to select
rows from the “jobs” table where the “status” column is not equal to
“X”.
Is there a way to apply the “status != ‘X’” condition (similar to a
condition on a find or a has_many declaration) to the object or do I
have to handle this explicitly in whatever find() calls I make on the
object?
Obviously, one way to go is to just make a view that does my filtering
for me.
I’ve seen with_scope. This is limited to a particular block correct?
What I’m hoping for is some kind of “default” scope that will always be
applied to find calls.
This looks promising, but it is unclear to me how to use it from the
documentation. There’s no example of how to declare the plugins use in
a controller.
Actually, let me illustrate with a section of my own code:
(instance method of a Group model)
def members
User.with_scope :find => {:conditions => “#{User.table_name}.id !=
#{self.leader.id}”} do
users.find_all
end
end