Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I never tried to do this but you may want to try the following (I just changed how the aliases are done):</p> <pre><code>class Property &lt; ActiveRecord::Base has_and_belongs_to_many :property_values, :extend =&gt; 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 </code></pre> <p>If it does not work here is another idea you may wanna try:</p> <pre><code>class Value &lt; ActiveRecord::Base self.abstract_class = true end class ExtendedValue &lt; Value end class ExtendedValue2 &lt; Value end class Property &lt; ActiveRecord::Base has_and_belongs_to_many :property_values, :class_name =&gt; 'ExtendedValue' has_and_belongs_to_many :property_values_extended, :class_name =&gt; 'ExtendedValue' has_and_belongs_to_many :property_values_extended2, :class_name =&gt; 'ExtendedValue2' end </code></pre> <p>The idea is to have one hatbm association per "type" (if you can group your extensions that way) and use the one you want at a given time, if you can do what you want that way I am also pretty sure it will have a smaller impact performance than patching every returned object after activerecord returned them.</p> <p>I am kinda curious at what you are trying to achieve with this :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload