Hi Guyren,
Since your using a class method you need to use a class variable to
store the value:

@model_name_cache needs to be @@model_name_cache

Rob

On Wed, Feb 17, 2010 at 07:43, Bradly <[email protected]> wrote:
> I think this is caused by using ||= on variables in a class method.
>
> Hope that helps!
>
> Thanks,
> Brad
>
>
> On Wed, Feb 17, 2010 at 12:03 AM, Guyren G Howe <[email protected]> wrote:
>>
>> I've got some code I've been using for a while, which is a generic
>> controller so I don't need endless identical controller methods in my basic
>> controller classes.
>>
>> In my generic new, I do this:
>>
>>  def new
>>   @thing = my_model.new
>>  end
>>
>> and my_model winds up in this:
>>
>>  def self.model_from_controller_name(name)
>>   (@model_name_cache ||= {})[name] ||=
>> table_name_from_controller_name(name).singularize.camelize.constantize
>>  end
>>
>> and all of this works reasonably, except that the second time I use my
>> controller this way, the instance I get back fails to do all sorts of basic
>> stuff. For example, it fails to call the id method on ActiveRecord::Base —
>> instead, it gets stuck in a recursive loop in method_missing.
>>
>> This would appear to be something to do with some non-obvious class
>> reloading behavior, perhaps because I'm in development mode. If I take the
>> caching out, the problem disappears.
>>
>> Should I just give up on the caching here? I'd like to understand why
>> caching a reference to a model class should fail.
>>
>> (BTW, this relates to a previous post earlier this evening; I've now tied
>> it down more, so I thought I'd start a new topic).
>>
>> --
>> SD Ruby mailing list
>> [email protected]
>> http://groups.google.com/group/sdruby
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby



-- 
Rob Kaufman
http://notch8.com
gtalk/jabber/email: [email protected]
p: 858-412-7749
f: 866-477-1620

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to