Adding methods to an array of AR objects

I have some methods that i want to be able to call on any collection of
ActiveRecord objects. What’s the ‘right’ way to set this up? I don’t
want to just monkey patch Array as it’s specific to an AR array.

Thanks
max

On 7 Nov 2008, at 10:14, Max W. wrote:

I have some methods that i want to be able to call on any collection
of
ActiveRecord objects. What’s the ‘right’ way to set this up? I don’t
want to just monkey patch Array as it’s specific to an AR array.

If you extend AssociationCollection you’ll be able to it on any
association (eg some_object.customers.foo). There is no proxy object
for the result of Foo.find though (I have an experimental plugin that
does do this). You could wrap the result of finds in a proxy (as i
did) or you could add singleton methods to the results of find
(ie

class << ActiveRecord::Base
def find_with_extra_methods(*args)
results = find_without_extra_methods(*args)
results.extend(SomeModuleOfExtraMethods) if results.is_a? Array
results
end

alias_method_chain :find, extra_methods
end