# Model classes in different folders

**URL:** <https://www.ruby-forum.com/t/model-classes-in-different-folders/57562>\
**Category:** Rails\
**Created:** [April 12, 2006, 1:10am UTC](https://www.ruby-forum.com/t/model-classes-in-different-folders/57562 "2006-04-12T01:10:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chuvisco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@chuvisco](https://www.ruby-forum.com/u/chuvisco)\
**Post date:** [April 12, 2006, 1:10am UTC](https://www.ruby-forum.com/t/model-classes-in-different-folders/57562/1 "2006-04-12T01:10:13Z")

</div>

Hello,

I have the following structure:

- app
  - controllers
    - helpdesk
      - call\_controller.rb (Helpdesk::CallController \<  
ApplicationController)

  - models
    - employee.rb
    - department.rb (Department \< ActiveRecord::Base)
    - helpdesk
      - call.rb
      - call\_history.rb (Helpdesk::CallHistory \< ActiveRecord::Base)

Is it possible to have a relationship between call\_history and  
department? The problem is that they are in different folders (root and  
helpdesk, respectively). I was thinking the mapping could be like this:

- in call (has\_many :call\_history \<- it’s working)

- in call\_history ( has\_one: department \<- it’s not working)

Could you help me?

Thanks in advance for any help!

Marcia

---

<div class="post-metadata">

**Author:** ![chuvisco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@chuvisco](https://www.ruby-forum.com/u/chuvisco)\
**Post date:** [April 12, 2006, 1:38am UTC](https://www.ruby-forum.com/t/model-classes-in-different-folders/57562/2 "2006-04-12T01:38:39Z")

</div>

Marcia A. wrote:

> Could you help me?

Move call.rb and call\_history.rb up into the models folder 🙂  
Dan

---

<div class="post-metadata">

**Author:** ![chuvisco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@chuvisco](https://www.ruby-forum.com/u/chuvisco)\
**Post date:** [April 12, 2006, 1:54am UTC](https://www.ruby-forum.com/t/model-classes-in-different-folders/57562/3 "2006-04-12T01:54:42Z")

</div>

why would you want to do this? It seems like you’re just asking for  
trouble  
by not following the rails standard for very little gain.

---

<div class="post-metadata">

**Author:** ![chuvisco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@chuvisco](https://www.ruby-forum.com/u/chuvisco)\
**Post date:** [April 12, 2006, 5:06am UTC](https://www.ruby-forum.com/t/model-classes-in-different-folders/57562/4 "2006-04-12T05:06:25Z")

</div>

Hi Marcia,

a couple of points:

1 - if you’re not using rails1.1 this is probably going to cause you  
grief - I wouldn’t bother.  
2 - the simplest thing is to keep everything in the toplevel namespace  
3 - there may come a point when your app is complex enough that the  
complexity of namespaces is worth it to reduce clutter in your models  
directory _but_…  
4 - it’s not worth it (imho) unless you’re being crushed under a  
bazillion files in /app/models

That said, it is possible to do what you want, EXCEPT your use of a  
‘helpdesk’ directory in controllers _and_ a ‘helpdesk’ directory in  
models. They will clash (at least they did the last time I had to  
use namespaces) and there’s pretty much no way around it other than  
to have different directory names for app/controllers and app/models.

So, just concentrating on the models, this ought to work:

in app/models/helpdesk/call.rb

class Helpdesk::Call \< ActiveRecord::Base  
has\_many :call\_history

# works because ‘Helpdesk’ module is implied

# and call\_history.rb is in the helpdesk directory

end

in app/models/helpdesk/call\_history.rb

class Helpdesk::CallHistory \< ActiveRecord::Base  
has\_one :department, :class\_name =\> ‘::Department’

# you have to tell rails _exactly_ where to find the

# class you are referring to if it’s not in the current module

# namespace

end

Note the :: in front of Department - it tells rails to look in the  
toplevel namespace for the class named ‘Department’ - if you didn’t  
have the ‘::’ characters it would assume the class is in the  
‘Helpdesk’ module.

Also note that if, for example, Department (app/models/department.rb)  
defines a relationship to something in the helpdesk directory then it  
will need to fully specify the :class\_name argument as well.

I hope this helps,

## Regards, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
