Marko Anastasov wrote this on February 22, 2009
Namespacing models
Namespacing your Rails models is a worth considering if you feel that they’ve started to pile up, and can be logically divided into subdirectories. Recently we had our Category model run into a name conflict with the one that’s generated by youtube-model which required us to this.
Like most things in Rails it takes very few steps to make it work. Say you want your existing Category to become Event::Category. Move the model file to app/models/event/
, put set_table_name "categories"
inside the class, which will also keep your model associations working, and you’re done. Mike Mondragon wrote a good overview of how to use and test namespaced models with single table inheritance and polymorphic associations.