Apologies if this is hard to follow.
Previous to Rails 2.3, we have been placing models in subdirectories
for organizational purposes, following the suggestions in
http://m.onkey.org/2007/12/9/namespaced-models, and setting:
<code> config.load_paths +=
Dir["#{RAILS_ROOT}/app/models/[a-z]*"] </
code>
The models are not namespaced, so e.g. app/models/foos/foo.rb defines
"Foo", not "Foos::Foo". FWIW, we have avoided namespacing
our models
due to numerous posts that state it is buggy, and from our own
experiences of the same.
This worked OK, except for two issue: (1) we would occasionally get
"Expected app/models/foos/foo.rb to define Foos::Foo", and (2) models
in subdirectories loaded like this would not auto-reload in
development mode after they were changed.
First, we fixed (1) by explicitly loading each file with the 3rd code
sample (see below), but it didn''t fix (2). So we managed to fix both
with this bit of code (from http://www.ruby-forum.com/topic/124830#633802)
to get around that issue, placed at the bottom of application.rb
<code>
model_folders = %w(foos bars bazs)
for folder in model_folders do
Dir["#{RAILS_ROOT}/app/models/{folder}/*.rb"].each { |file|
require_dependency "{folder}/#{file[file.rindex(''/'') +
1...-3]}"
}
end
</code>
So with both of these, we were able to load models in directories
other than app/models reliably, and also have them auto-reload changes
in development mode. However, with Rails 2.3.1 RC2 this strategy is
no longer working. The 2nd code segment does not seem to reliably
load model classes in subdirectories, again resulting in errors
similar to: "Expected app/models/foos/foo.rb to define Foos::Foo" on
the 2nd and subsequent request.
If we remove the 2nd code segment above, and replace it with this 3rd
code segment to explicitly load each file, a suggestion from this post
(http://www.paperplanes.de/2007/5/2/
namespacing_your_rails_model_an.html)
<code>
[ "app/models" ].each do |path|
Dir["#{RAILS_ROOT}/#{path}/**/*.rb"].each do |file|
load file
end
end
</code>
the errors go away, but again we are left without being able to auto-
reload those models in development mode, so changes to those models
require restarting the application (we are running on Passenger) which
takes away one of the nicest features of doing Rails development IMHO.
Would appreciate any suggestions for being able to have our cake and
eat it too. Life was better before trying to organize our models
into subdirectories.
Thanks,
Michael
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---