The odd thing is that I get away with over-riding [] and []= until I try
defining an association with a pre-fetch :include that involves the
model with the over-ridden [] and []=. And googling around, I can see
several people suggesting over-ridding [] and []= on an AR model to
accomplish various clever things. So maybe it is a bug? Regardless, I
think I'll try to refactor to do not do that to avoid the risk of
getting in the way of AR. It seems to be rather easier than one would
like to get in the way of AR.
In particular, the too-clever thing I was doing was having an AR that
could take arbitrary keys and either store them in an attribute, if it
exists, or in a serialized hash, if otherwise. An excerpt of the
relevant stuff:
def MyThing < ActiveRecord::Base
serializes :my_data
def [](key)
if ( has_attribute?(key) )
self.send(key)
else
my_data[key]
end
end
def []=(key, value)
if ( has_attribute?(key) )
self.send(key.to_s+'=', value)
else
my_data[key] = value
end
end
All well and good--until I try to define an association with :include =>
MyThing .
Jonathan Rochkind wrote:
> Yeah, you're right, sorry, thanks so much for your help. Probably for
> the best that i changed the model name from Request anyway, might as
> well commit that to my svn since it's advertised as a Rails reserved
> word.
>
> I just posted this to the forum with a different subject, since it ended
> up having nothing to do with this, but. But I was forced to really get
> down and dirty with my testing, and it turns out it's because I had
> over-ridden [] and []= on one of the models involved. I got away with
> that in Rails 1 (perhaps AR didn't used to use these methods?), but not
> in Rails 2. Okay, more refactoring, this one's even more of a pain then
> the last one. Oh well, I guess I was being too clever before.
>
> Jonathan
>
> Frederick Cheung wrote:
>> On 10 Nov 2008, at 21:04, Jonathan Rochkind wrote:
>>
>>>
>>> Hmm, just changing the model name from Request to AppRequest doesn't
>>> seem to have done it.
>>>
>>> I tried to leave the (many) associations pointing to AppRequest (nee
>>> Request) the same, but specify a :class_name and :foreign_key.
>>>
>>> It sounds like maybe "request" as the name of an assocation, even when
>>> not the name of the model, is a no-no too?
>>
>> I'm not convinced that has anything to do with this at all. If you
>> could post your teeny tiny example I might be able to work out what is
>> going in (given that I wrote the code behind include in 2.1.2)
>>
>> Fred
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---