# Completely override has\_many accessor

**URL:** <https://www.ruby-forum.com/t/completely-override-has-many-accessor/165784>\
**Category:** Rails\
**Created:** [April 17, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/completely-override-has-many-accessor/165784 "2009-04-17T12:28:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [April 17, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/completely-override-has-many-accessor/165784/1 "2009-04-17T12:28:38Z")

</div>

Hi,

I have an unusual use-case where I need to override a has\_many  
accessor to return an array of results depending on some the value of  
a class variable. I’ve tried several solutions, the closest to success  
using the extension option:

```auto
has_many :meta_associations, :as => :meta_extendable do
          # Find any meta_associations that are joined to this
activerecord instance
          def meta_associations(*args)
            options = args.extract_options!

            if proxy_owner.class.applicable_type ==
proxy_owner.class.name
              options[:conditions] ||= { :meta_extendable_id =>
proxy_owner.id,
                                         :meta_extendable_type =>
proxy_owner.class.applicable_type }
            else
              options[:conditions] ||= { :meta_extendable_id =>
proxy_owner.send
(proxy_owner.class.applicable_type_foreign_key.to_sym),
                                         :meta_extendable_type =>
proxy_owner.class.applicable_type }
            end

            MetaAssociation.find(args.first, options)
          end

        end
```

However, this just gives me an  
instance.meta\_associations.meta\_associations method, rather than  
overriding the default instance.meta\_associations. Is this possible,  
or should I forget using :has\_many and manually write all the  
collection accessors/mutators?

## Thanks, Chris

[www.chrisblunt.com](http://www.chrisblunt.com)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [April 17, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/completely-override-has-many-accessor/165784/2 "2009-04-17T14:24:12Z")

</div>

On 17 Apr 2009, at 11:27, Chris wrote:

> However, this just gives me an  
> instance.meta\_associations.meta\_associations method, rather than  
> overriding the default instance.meta\_associations. Is this possible,  
> or should I forget using :has\_many and manually write all the  
> collection accessors/mutators?

Well you cannot overwrite the instance.meta\_associations method like  
that (since you’re adding method to the associations proxy, but  
foo.meta\_associations just returns that proxy).

You may get some mileage out of overwriting the methods that actually  
load the association (find\_target, the method that generates the scope  
used for other operations etc)

Fred

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [April 18, 2009, 5:10pm UTC](https://www.ruby-forum.com/t/completely-override-has-many-accessor/165784/3 "2009-04-18T17:10:18Z")

</div>

Hi Fred,

Thanks for your reply. I tried overriding the methods directly, but  
they seem to be ignored by AR and the association proxy takes  
precedence over my implementation. However, I’ve looked over my code  
and am going to try a different approach than trying to override the  
original accessors for now; I’ll post back how I get on.

Thanks,  
Chris

On Apr 17, 1:23 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
