One thing I'd like to add. Caching this sort of thing, while it might by slightly quicker, is barely going to save any time at all. The complexity of Rails and all the infrastructure makes a few string operations and an eval pretty small in comparison and it might just be worth ignoring completely.
Martin Emde On Wed, Feb 17, 2010 at 8:38 PM, Martin Emde <[email protected]> wrote: > Guyren, > > You can use @instance variables in classes but they don't work with > inheritance. > http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/is > a pretty good explanation. > > What you're doing actually requires a class @@variable (or a constant). You > want one hash that exists in every controller that's always the same. > > Here's some code I just made up that may or may not work :) I think Hash > with default is pretty ideal for what you're doing. > > MODELS = Hash.new { |name| > table_name_from_controller_name(name).singularize.camelize.constantize > } > MODELS['ThingsController'] > > I feel ok using a constant here because really this is constant data that's > just not all cached yet. You could also do it with @@vars in just about the > same way: > > @@models_by_name = Hash.new { |name| ... etc ... } > def self.models_by_name(name) > @@models_by_name[name] > end > > Hope that helps, > > Martin > > On Wed, Feb 17, 2010 at 3:01 PM, Guyren G Howe <[email protected]> wrote: > >> On Feb 17, 2010, at 13:17 , Rob Kaufman wrote: >> >> > 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 >> >> >> No, that wasn't it. I could see the second time around that I had the >> class in there; it's just that the class wasn't working properly. >> >> I suspect that the class reloading behavior in design mode was getting >> interrupted by my holding onto a reference to the class. >> >> Regards, >> >> Guyren G Howe >> Relevant Logic LLC >> >> guyren-at-relevantlogic.com ~ http://relevantlogic.com ~ +1 512 784 3178 >> >> Ruby/Rails, REALbasic, PHP programming >> PostgreSQL, MySQL database design and consulting >> Technical writing and training >> >> Read my book, Real OOP with REALbasic: < >> http://relevantlogic.com/oop-book/about-the-oop-book.php> >> >> -- >> SD Ruby mailing list >> [email protected] >> http://groups.google.com/group/sdruby >> > > -- SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby
