I never tried to do this, but you can try the following (I just changed the way aliases are executed):
class Property < ActiveRecord::Base has_and_belongs_to_many :property_values, :extend => PropertyUtil::Extensible def enrich(to_extend) modules.split(/\s*,\s*/).each do |mod| to_extend.extend(Properties.const_get(mod.to_sym)) end end end module PropertyUtil module Extensible def self.extended(mod) mod.module_eval do alias_method :old_find, :find alias_method :find, :new_find end end def new_find(*args) old_find(*args).map{|prop| proxy_owner.enrich(prop)} end end end
If this does not work, this is another idea you can try:
class Value < ActiveRecord::Base self.abstract_class = true end class ExtendedValue < Value end class ExtendedValue2 < Value end class Property < ActiveRecord::Base has_and_belongs_to_many :property_values, :class_name => 'ExtendedValue' has_and_belongs_to_many :property_values_extended, :class_name => 'ExtendedValue' has_and_belongs_to_many :property_values_extended2, :class_name => 'ExtendedValue2' end
The idea is to have one hat association per type (if you can group your extensions this way) and use the one you want at a given time, if you can do what you want, I'm sure it will have less impact performance than fixing each returned object after activerecord returns them.
I'm curious what you are trying to achieve with this :)
source share