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

Marko co-founded Rendered Text. He’s a programmer with a passion of creating something for other people, coupled with interests in how things work in the society and nature. He is having a great time working across many areas in the company, including product design, helping the engineering teams, empowering users and marketing. He likes to counterbalance it all with running, cycling or a day in the woods.

Suggested Reads

Inject is a fundamental building block

Inject is one of the fundamental, and most versatile constructs available in functional languages. It can be used to implement map, select, max, all? and a bunch of other iteration related methods. Unfortunately, many programmers are not aware of its awesome powers. This article is here to improve this fact.

Contact

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.

Rendered Text
Svetozara Miletica 10
21000 Novi Sad
Serbia