Forum: RSpec Trouble with (Rails) Nested Model Directories

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mudphone (Guest)
on 2009-03-06 10:36
(Received via mailing list)
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
David C. (Guest)
on 2009-03-06 12:42
(Received via mailing list)
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?
mudphone (Guest)
on 2009-03-07 17:10
(Received via mailing list)
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
David C. (Guest)
on 2009-03-07 17:14
(Received via mailing list)
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
This topic is locked and can not be replied to.