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.

comments powered by Disqus

About Marko Anastasov

Rendered Text co-founder. Started with code, currently more focused on people and words. Foosball striker and entry-level cyclist.

Suggested Reads

Rails Testing Handbook

A new ebook on building test-driven Rails apps with RSpec and Cucumber.

At Rendered Text, we have a long history with Ruby on Rails. Checking the blog archive reminds me that we published first posts about working with Rails way back in 2009.

———

Rendered Text is a software company. For questions regarding Semaphore, please visit semaphoreci.com. Otherwise, feel free to get in touch any time by sending us an email.