I still do not know exactly what was happening (and would be interested to know), however...
I did think that maybe the problem is that I have models that inherit from Sample and/or that I have controllers/models referencing constants in other models, and it is this that is confusing Rails. However, I have moved all the constants to a file in config/initializers (with a new module), and it still happens with classes that do not inherit from custom models. The issue is related to how Rails caches models. If I set config.cache_classes = true in config/environment/development.rb, then it works fine (conversely, setting config.cache_classes = false in config/environment/test.rb causes tests to fail). Alternatively, I can require the relevant models in a file in config/initializers. Either way, Rails keeps those models loaded, and it works okay. When caching is turned off, Rails seems to load the file, use it, and forget it. When another file tries to reference that class, Rails ignores load_paths, and looks in the wrong place. Anyway, a way around it is to require the files in a file in config/initializers, so at least I am up and running now. Andy Joel wrote: > Thanks for the reply >> On Thu, May 6, 2010 at 6:58 AM, Andy Joel <[email protected]> wrote: >>> I have my models and controllers set up in sub-folders, so, for example, >>> sample.rb is in app/models/sample_log, and samples_controller.rb is in >>> app/controllers/sample_log. The controllers are defined to be within a >>> namespace just though the class name, eg like this: >>> >>> class SampleLog::SamplesController < SampleLog::SuperController >>> >>> However, the models are not. >>> >>> class Sample < ActiveRecord::Base >> >> >> Where is this file? I'm guessing that it's in >> app/models/sample_log/sample.rb ? > Yes >> >>> Instead, I have added the relevant directories to config.load_paths in >>> environment.rb. >> >> Which may be part of the problem. You might well be fighting >> convention over configuration. >> >>> Expected R:/samplelog/app/models/sample_log/sample.rb to define >>> SampleLog::Sample >> >> What this is saying is that Rails tried to find a class or module >> SampleLog::Sample > Is that right? I think it is the other way around; it is trying to find > Sample, but objects because it thinks it should be in sample.rb, not > sample_log/sample.rb. > > "SampleLog::Sample" is not used anywhere in the project (and I have done > a search to confirm this). > >> >> What rails does here is to look for samplelog/sample.rb somewhere in >> the various load paths. It's finding app/models/sample_log/sample.rb >> which if it indeed contains: >> >> class Sample < ActiveRecord::Base >> >> outside of a class or module block is definining ::Sample, not >> ::SampleLog::Sample. >> >> I'm guessing that moving app/models/sample_log/sample.rb to >> app/models/sample.rb will fix this. > Yes, I think this is true (indeed, this is how it was originally set > up). However, I was HOPING to be able to split the models into sections. > >> >> If not then show us the walkback you get on the error, it should point >> to some piece of code which is referring to some constant >> (name_space::)*Sample > > > R:/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:428:in > > `load_missing_constant' > R:/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:77:in > > `const_missing_with_dependencies' > R:/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:439:in > > `load_missing_constant' > R:/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:93:in > > `const_missing' > app/controllers/sample_log/samples_controller.rb:11:in `home' > > Here are lines 10 to 12 > > def home > @samples = Sample.find :all > end -- 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.

