# Using modules as namespaces within an engine

**URL:** <https://www.ruby-forum.com/t/using-modules-as-namespaces-within-an-engine/66645>\
**Category:** Engines\
**Created:** [August 28, 2006, 5:47am UTC](https://www.ruby-forum.com/t/using-modules-as-namespaces-within-an-engine/66645 "2006-08-28T05:47:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caddleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/41988e/32.png) [@caddleman](https://www.ruby-forum.com/u/caddleman)\
**Post date:** [August 28, 2006, 5:47am UTC](https://www.ruby-forum.com/t/using-modules-as-namespaces-within-an-engine/66645/1 "2006-08-28T05:47:51Z")

</div>

Hi folks,

In writing an engine, it seems to me that I’d want to put all my model  
and controller classes within a namespace, to avoid name clashes with  
similarly named classes in the base application, or other engines. By  
namespaces, I mean wrap the class in a module.

For example, I’m writing a forum engine, and I have two models.

Forum::Thread  
Forum::Comment

And I’ve called their tables in the database:

forum\_threads  
forum\_comments

I think this will prevent clashes nicelly, and rails handles it by  
creating a /views/forum/ directory for my views. This all works fine  
as a rails app, and it runs fine too when I move it into an engine,  
except for loading fixtures in tests.

Rails does not have support for looking in a sub directory of  
“fixtures”, so i need to call my fixture files:

forum\_threads.yml  
forum\_comments.yml

Then, I do the following in my test case:

fixtures :forum\_threads, :forum\_comments  
set\_fixture\_class :forum\_comments =\> Forum::Comment  
set\_fixture\_class :forum\_threads =\> Forum::Thread

This works in a normal rails app, but not in an engine. All my tests  
do something like this:

1. Error:  
test\_create\_replaces\_form\_if\_invalid(Forum::ThreadControllerTest):  
FixtureClassNotFound: The class “ForumThread” was not found.  
method find in fixtures.rb at line 403  
method instantiate\_fixtures in testing\_extensions.rb at line 86  
method instantiate\_fixtures in testing\_extensions.rb at line 85  
method silence in base.rb at line 794  
method instantiate\_fixtures in testing\_extensions.rb at line 84  
method instantiate\_fixtures in testing\_extensions.rb at line 317  
method instantiate\_fixtures in testing\_extensions.rb at line 316  
method setup\_with\_fixtures in fixtures.rb at line 525  
method setup in fixtures.rb at line 547

Does anyone know of any difference in the way that namespaces (nested  
modules) work in engines compared to normal apps? I’m using rails  
1.1.6

cheers,

Craig  
[www.craigambrose.com](http://www.craigambrose.com)

---

<div class="post-metadata">

**Author:** ![caddleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/41988e/32.png) [@caddleman](https://www.ruby-forum.com/u/caddleman)\
**Post date:** [February 4, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/using-modules-as-namespaces-within-an-engine/66645/2 "2007-02-04T15:15:08Z")

</div>

Hi Craig, did you ever figure this out? I seem to have the same problem  
where my tests are not able to load fixtures for a name-spaced model. I  
get the same error:

FixtureClassNotFound: The class “MyappCountry” was not found.

(my model is Myapp::Country)

Ingo

---

<div class="post-metadata">

**Author:** ![caddleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/41988e/32.png) [@caddleman](https://www.ruby-forum.com/u/caddleman)\
**Post date:** [April 19, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/using-modules-as-namespaces-within-an-engine/66645/3 "2007-04-19T23:51:59Z")

</div>

I have this issue as well. I noticed that the namespace models are not  
being found properly or the framework is not finding the fixture class  
name check out issue #5157

if you have a solution I’d love to hear it!

Ingo W. wrote:

> Hi Craig, did you ever figure this out? I seem to have the same problem  
> where my tests are not able to load fixtures for a name-spaced model. I  
> get the same error:
> 
> FixtureClassNotFound: The class “MyappCountry” was not found.
> 
> (my model is Myapp::Country)
> 
> Ingo

---

<div class="post-metadata">

**Author:** ![caddleman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/41988e/32.png) [@caddleman](https://www.ruby-forum.com/u/caddleman)\
**Post date:** [April 21, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/using-modules-as-namespaces-within-an-engine/66645/4 "2007-04-21T18:44:13Z")

</div>

My solution was

require File.dirname( **FILE** ) + ‘/…/…/test\_helper’

class Messaging::BulletinTest \< Test::Unit::TestCase

Test::Unit::TestCase.fixture\_path= File.dirname( **FILE** )  
+"/…/…/fixtures/messaging"

fixtures :messaging\_bulletins

set\_fixture\_class :messaging\_bulletins =\> Messaging::Bulletin

def test\_database  
assert\_equal messaging\_bulletins(:one).id, 1  
end  
end

notice the Test::Unit::TestCase.fixture\_path function sets the path to  
the fixtures which I want to use in this test case.

then I assign the fixture class to my namespaced model.

cheers!  
Tim

Tim G. wrote:

> I have this issue as well. I noticed that the namespace models are not  
> being found properly or the framework is not finding the fixture class  
> name check out issue #5157
> 
> if you have a solution I’d love to hear it!
> 
> Ingo W. wrote:
> 
> > Hi Craig, did you ever figure this out? I seem to have the same problem  
> > where my tests are not able to load fixtures for a name-spaced model. I  
> > get the same error:
> > 
> > FixtureClassNotFound: The class “MyappCountry” was not found.
> > 
> > (my model is Myapp::Country)
> > 
> > Ingo
