# Modules and models

**URL:** <https://www.ruby-forum.com/t/modules-and-models/170785>\
**Category:** Rails\
**Created:** [June 22, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/modules-and-models/170785 "2009-06-22T20:43:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [June 22, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/modules-and-models/170785/1 "2009-06-22T20:43:57Z")

</div>

We have decided to wrap each of our model classes in a module, where  
the module name is currently the same across all models and is the  
name of the application. So, for example, we have:

module SpringBase  
class User \< ActiveRecord::Base  
…  
end  
end

My reading suggests that I should site all model files in the  
directory app/models/spring\_base/ to, I hope, help Rails to find them  
when I have code such as user=SpringBase::User.new in my controller  
logic.

Is this the correct approach?

Thanks.

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [June 22, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/modules-and-models/170785/2 "2009-06-22T21:57:54Z")

</div>

Lee wrote:

> We have decided to wrap each of our model classes in a module, where  
> the module name is currently the same across all models and is the  
> name of the application.  
> […]

Why?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [June 23, 2009, 11:26am UTC](https://www.ruby-forum.com/t/modules-and-models/170785/3 "2009-06-23T11:26:10Z")

</div>

We want to namespace our code to avoid conflicts with any framework  
code. For example, we had a method called ‘changed’ in our models  
which clashed with an ActiveRecord-based rmethod of the same name and  
this led to many hours of debugging.

My research suggests it is valid to namespace models and that one  
should use the same approach as for namespacing controllers.

On 22 June, 20:57, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![lee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@lee](https://www.ruby-forum.com/u/lee)\
**Post date:** [June 23, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/modules-and-models/170785/4 "2009-06-23T17:58:59Z")

</div>

Lee wrote:

> We want to namespace our code to avoid conflicts with any framework  
> code.

I don’t see how this will help. Each model is _already_ its own  
namespace, so further namespacing them won’t make a difference in this  
regard.

> For example, we had a method called ‘changed’ in our models  
> which clashed with an ActiveRecord-based rmethod of the same name and  
> this led to many hours of debugging.

It will still clash, because your models will still inherit from  
ActiveRecord::Base.

> My research suggests it is valid to namespace models and that one  
> should use the same approach as for namespacing controllers.

I’ve never tried doing this, but I suspect that you’re going about it  
backwards. You’re trying to include the model in the module, which will  
not break up the model’s namespace; what you may need to do is include  
the module into the model instead.

However, I think I would advise against this. If your model methods are  
conflicting with framework methods, it’s probably better to rename them  
– it will be less confusing.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
