TylerRick/has_and_belongs_to_many_with_deferred_save — Issue #4: memcached and singleton methods
Author: neogrande
Created: 2012-02-21 13:41:55+00:00
State: open

This may be a little to specific for fixing here, but maybe other people are running into this as well.
Basically, the problem is:

```ruby
class SomeModel < ActiveRecord::Base
has_and_belongs_to_many :multi
end

class OtherModel < ActiveRecord::Base
has_and_belongs_to_many_with_defered_save :multi
end

s = SomeModel.new
Marshal.dump(s.multi) # will work
o = OtherModel.new
Marshal.dump(o.multi) # won't work, because o.multi is a proxy with singleton methods
```

We have Rails 2.3.5, i18n-Gem 0.3.6 with I18n-Caching with memcached and multiple I18n-Backends (yml-Files, Database).
If you add a validation like ```errors.add_on_blank(:multi)```, Rails will search for a :blank translation inside it's yaml files. If it doesn't find one in the scope "en.activerecord.errors.models.other_model.multi", it will create a ```MissingTranslationData``` object and searches for the message in another scope. Finally it will take the default "en.activerecord.errors.messages.blank".

But, in between, the ```MissingTranslationData``` object is written to the cache. And unfortunately it has a reference to the value, which was validated - our proxy object.
One can argue, that this temporary object shouldn't enter the cache or the reference to the proxy object isn't necessary. But in the end it works with the non-deferred assocation, but with the deferred it doesn't.

The "evil" code is, of course, the ```class_eval << "..."``` stuff.
Shouldn't it be possible, to overwrite them directly inside the AssociationProxys?
If I get the time, I will try to find a solution...