Hey,
to intercept dynamic finders, you just have to scope method_missing
class Klass
class << self
alias :find_unrestricted :find
def find(*options)
self.with_scope( :find => { :conditions =>
restriction_condition } ) do
super(*options)
end
end
alias :count_unrestricted :count
def count(*options)
self.with_scope( :find => { :conditions =>
restriction_condition } ) do
super(*options)
end
end
def method_missing(method_id, *arguments)
self.with_scope( :find => { :conditions =>
restriction_condition } ) do
super(method_id, *arguments)
end
end
protected
def restriction_condition
["#{self.table_name}.active = ?", true]
end
end
end
Lori O. wrote:
On 12/21/06, Aaron [email protected] wrote:
Unfortunately, I don’t know how to make the dynamic finders respect the
with_scope. You can avoid using dynamic finders, but that’s prone to
error. If you come up with something please let me know.
Aaron
Has anyone got any ideas on this one? I have a database table that is
never
accessed on it’s own, always by it’s relationships to other tables. So
I
need a series of models, all pointing at the same table, but that
operate as
if they are within a “with_scope”.
Just to give a fer-instance…
Table People. Table Homes with foreign key owner, pointing to People.
I am
not interested in People. I am only interested in HomeOwners.
I need something like
class Person < ActiveRecord::Bass
end
class HomeOwner < Person
def self.find(*args)
with_scope( :find =>{ :conditions => “id in (select distinct(owner)
from
homes)” } ) do
super(*args)
end
end
end
Only I need it to be scoped for all the dynamic finders, and count, etc.
Regards, Lori