Putting filter conditions on ActiveRecord::Base object?

All,

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.

Thanks,
Wes

Can I use an after_find callback to cull out the rows that I don’t want?

Wes G. wrote:

Can I use an after_find callback to cull out the rows that I don’t want?


Posted via http://www.ruby-forum.com/.

Take a look for with_scope…

_Kevin

Kevin,

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.

Wes

This looks promising.

http://roman2k.free.fr/rails/meantime_filter/0.1.0/rdoc/

Anyone want to encourage/discourage me from using it?

Wes

Look on http://www.agilewebdevelopment.com/plugins/global_scope and you
will find exactly what you need.

Dimitrij

PS: Pease feel free to participate in the following discussion:
http://www.ruby-forum.com/topic/85781

Dim wrote:

Look on http://www.agilewebdevelopment.com/plugins/global_scope and you
will find exactly what you need.

Dimitrij

PS: Pease feel free to participate in the following discussion:
Fix for the multiple 'find' aliasing side-effects - Rails - Ruby-Forum

Dimitri -

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.

Wes

Use acts_as_view (Google for it) - by maiha, author of scoping in the
first place.

Dimitri,

It looks like your plugin is for a plugin author to use to ensure that
they don’t cause method invocation errors due to custom scoping rules.

Maybe I’m misunderstanding - I just want to scope my controller methods
and it’s unclear how I take advantage of global_scope to do that.

WG

Wes G. wrote:

Dimitri,

It looks like your plugin is for a plugin author to use to ensure that
they don’t cause method invocation errors due to custom scoping rules.

Yes, that was my original reason.

Maybe I’m misunderstanding - I just want to scope my controller methods
and it’s unclear how I take advantage of global_scope to do that.

If you want to add scope to some controller methods, global scopes are
not the right tool. Why not doing something like that:

def some_action_that_should_use_my_scope(…)
with_my_scope do

end
end

def some_other_action_that_should_use_my_scope(…)
with_my_scope do

end
end

protected
def with_my_scope(&block)
with_scope(:find=>{…}, :merge, &block)
end

Dimitrij

Wow, there are so many different solutions to this - I wonder if
controller level scoping will be added in some way to 1.2?

with_scope is going to be your best bet here. Apply the KISS rule
liberally.

Erik

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

with_scope is going to be your best bet here. Apply the KISS rule
liberally.

Erik