Modules and models

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.

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
[email protected]

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-

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
[email protected]