Forum: Ruby on Rails I want to stick my models in a module

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.
Pat M. (Guest)
on 2006-06-07 23:38
(Received via mailing list)
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:

  4) 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
Pat M. (Guest)
on 2006-06-07 23:41
(Received via mailing list)
On 6/7/06, Pat M. <removed_email_address@domain.invalid> wrote:
>   4) 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.
Pat M. (Guest)
on 2006-06-07 23:58
(Received via mailing list)
On 6/7/06, Pat M. <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
Alex W. (Guest)
on 2006-06-08 01:00
Pat M. wrote:
> On 6/7/06, Pat M. <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.
Pat M. (Guest)
on 2006-06-08 01:33
(Received via mailing list)
On 6/7/06, Alex W. <removed_email_address@domain.invalid> wrote:
> >
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> 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
Pat M. (Guest)
on 2006-06-08 01:58
(Received via mailing list)
On 6/7/06, Pat M. <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
Pat M. (Guest)
on 2006-06-08 08:06
(Received via mailing list)
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
This topic is locked and can not be replied to.