Trouble with (Rails) Nested Model Directories


#1

Hi folks,

In order to bring some order to the many model classes that I have
under /app/models, I’m created a new directory (for example):
/app/models/nestdir

I’ve added the required configuration line in environment.rb:
config.load_paths += %W( #{RAILS_ROOT}/app/models/nestdir )

But, when I run my specs, I get an error such as:

dependencies.rb:428:in `load_missing_constant’: Expected RAILS_ROOT/
app/models/nestdir/my_model.rb to define Nestdir::MyModel (LoadError)

From my understanding, the models in the dir listed in
config.load_paths should be available in the root Rails namespace. Is
this not how rspec handles things?

Is there a standard way of setting up specs for nested model
directories? The models are not nested within a parent module (like
Nestdir, which would have matched the directory name).

Thanks,
Kyle


#2

On Thu, Mar 5, 2009 at 4:02 PM, mudphone removed_email_address@domain.invalid wrote:


Nestdir, which would have matched the directory name).
I just created a fresh rails-2.3.0 app w/ the latest rspec (built from
git - 1.1.99.11) and not only was able to include a nested model, I
was able to run the specs under each of the following conditions:

  • neither spec nor model are namespaced

describe User do
end

class User < ActiveRecord::Base
end

  • model and spec are both namespaced

module Admin
describe User do
end
end

module Admin
class User < ActiveRecord::Base
end
end

  • config is modifed per your example above
  • config is NOT modifed per your example above

What versions of rspec and rails are you using?


#3

David,

Thanks for your help and for setting me straight. I am running Rails
2.2.2 and rspec 1.1.12.

I started a new Rails project as well, and have discovered that my
problem is that I have root-level model class, with the same name as
the nested directory. It looks like this:

app/models/thing.rb
app/models/thing/nested_thing.rb

This set-up causes errors. If I move the thing.rb class to the “thing”
directory, or rename the “thing” directory to something else, all is
good.

Again, thanks for your help.

Kyle


#4

On Sat, Mar 7, 2009 at 12:01 AM, mudphone removed_email_address@domain.invalid wrote:

app/models/thing/nested_thing.rb

This set-up causes errors. If I move the thing.rb class to the “thing”
directory, or rename the “thing” directory to something else, all is
good.

Again, thanks for your help.

You’re welcome. Glad you got it figured out.

Cheers,
David