# I want to stick my models in a module

**URL:** <https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758>\
**Category:** Rails\
**Created:** [June 7, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758 "2006-06-07T21:38:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 7, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758/1 "2006-06-07T21:38:47Z")

</div>

I’ve got enough models now that I’d like to separate them into  
modules. For example, I have the class Player that I want to stick in  
the Trainer module. I’ve changed the definition to

class Trainer::Player

I’ve created a models/trainer dir, test/unit/trainer, and  
test/fixtures/trainer. Change the PlayerTest class to be  
Trainer::PlayerTest. I get the following error when I try to run it:

1. Error:  
test\_validate\_range(Trainer::PlayerTest):  
FixtureClassNotFound: The class “Player” was not found.  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/fixtures.rb:403:in  
`find' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/fixtures.rb:478:in`players’  
test/unit/trainer/player\_test.rb:48:in `test\_validate\_range’

I’ve tried creating a model with script/generate model Trainer::Foo  
just to see if there was any difference, but it looks like I have it  
all right. Do I need to do any special class loading or something?  
By the way, I’ve also changed the test helper require to be require  
File.dirname( **FILE** ) + ‘/…/…/test\_helper’. I really want to group  
these into modules, so I’d appreciate any advice.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 7, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758/2 "2006-06-07T21:41:55Z")

</div>

On 6/7/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. Error:  
> all right. Do I need to do any special class loading or something?  
> By the way, I’ve also changed the test helper require to be require  
> File.dirname( **FILE** ) + ‘/…/…/test\_helper’. I really want to group  
> these into modules, so I’d appreciate any advice.
> 
> Pat

Also, I can access the model fine with Trainer::Player through  
console. So apparently it’s just this fixture loading problem.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 7, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758/3 "2006-06-07T21:58:15Z")

</div>

On 6/7/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > just to see if there was any difference, but it looks like I have it

Okay so I _didn’t_ actually move the players.yml fixtures file into  
the trainer subdir of fixtures. Did that, and now it just says that  
it can’t find test/fixtures/players. Well yeah, I don’t want it  
to…and the generate script automatically sticks the fixtures in the  
trainers subdir.

Hopefully someone knows how to do this. I can’t imagine that Rails  
doesn’t let you put your models into modules.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 7, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758/4 "2006-06-07T23:33:18Z")

</div>

On 6/7/06, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Well I just decided to try throwing a string in the fixtures instead  
of using the symbol…  
fixtures “trainer/player”

That worked for loading the fixtures. Won’t let me use the  
players(:first) method of getting something, so I’ve just had to do  
Player.find(1). I’m sure there’s a performance hit, but I don’t  
really care. I suppose there’s a chance this’ll break in some future  
version, but for now it works fine.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 7, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758/5 "2006-06-07T23:58:20Z")

</div>

On 6/7/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > trainers subdir.  
> > > –
> 
> That worked for loading the fixtures. Won’t let me use the  
> players(:first) method of getting something, so I’ve just had to do  
> Player.find(1). I’m sure there’s a performance hit, but I don’t  
> really care. I suppose there’s a chance this’ll break in some future  
> version, but for now it works fine.
> 
> Pat

Appears to work for some, not for others. Don’t know what the rule  
is, and I’m too tired to keep working on this. Still unsolved,  
looking for advice.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 7, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758/6 "2006-06-07T23:00:26Z")

</div>

Pat M. wrote:

> On 6/7/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > just to see if there was any difference, but it looks like I have it
> 
> Okay so I _didn’t_ actually move the players.yml fixtures file into  
> the trainer subdir of fixtures. Did that, and now it just says that  
> it can’t find test/fixtures/players. Well yeah, I don’t want it  
> to…and the generate script automatically sticks the fixtures in the  
> trainers subdir.
> 
> Hopefully someone knows how to do this. I can’t imagine that Rails  
> doesn’t let you put your models into modules.
> 
> Pat

I seem to remember seeing a bug about this. Everything works except  
fixtures. Can’t seem to find it now, but I think it was on there  
somewhere.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 8, 2006, 6:06am UTC](https://www.ruby-forum.com/t/i-want-to-stick-my-models-in-a-module/62758/7 "2006-06-08T06:06:27Z")

</div>

I managed to come up with some code that handles this. I know it  
would certainly break in some cases, but it works very well for me.  
Patch at [http://dev.rubyonrails.org/ticket/5332](http://dev.rubyonrails.org/ticket/5332)
