# Tables names with model classes in seperate modules

**URL:** <https://www.ruby-forum.com/t/tables-names-with-model-classes-in-seperate-modules/59925>\
**Category:** Rails\
**Created:** [May 8, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/tables-names-with-model-classes-in-seperate-modules/59925 "2006-05-08T19:12:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [May 8, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/tables-names-with-model-classes-in-seperate-modules/59925/1 "2006-05-08T19:12:41Z")

</div>

I have a module that deals with content pages and categories in my  
application so create following tables:

content\_pages  
content\_categories

Obviously I need a Page and Category module to map to these tables, but  
in order to avoid conflicts with other Category classes, i declare my  
model classes like this:

Content::Page  
Content::Category

Now, that’s all fine - except that my models are still looking for the  
tables based on the class name - i.e. pages or categories. The easy fix  
is to just use set\_table\_name in the models, but i wondered if there was  
some way that Rails can pick tables like this for example:

{module}\_{class}

Cheers!

Steve

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [May 8, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/tables-names-with-model-classes-in-seperate-modules/59925/2 "2006-05-08T19:46:42Z")

</div>

On May 8, 2006, at 10:10 AM, Stephen B. wrote:

> Content::Page  
> Content::Category
> 
> Now, that’s all fine - except that my models are still looking for  
> the tables based on the class name - i.e. pages or categories. The  
> easy fix is to just use set\_table\_name in the models, but i  
> wondered if there was some way that Rails can pick tables like this  
> for example:
> 
> {module}\_{class}

How about

module Content  
class Base \< ActiveRecord::Base  
self.abstract\_class = true  
self.table\_name\_prefix = ‘content\_’  
end

```
 # table_name = 'content_pages'
 class Page < Base
 end

```

end

Best,  
jeremy

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [May 8, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/tables-names-with-model-classes-in-seperate-modules/59925/3 "2006-05-08T20:43:29Z")

</div>

Hmm… the only problem with that is that Rails still looks for the model  
in models/content/page.rb rather than models/content.rb. I’m guessing i  
could override that but this whole approach is ringing alarm bells…

The other problem i have is with choosing a controller layout. I could  
do it like this:

controllers/admin/content/page\_controller.rb  
controllers/admin/content/category\_controller.rb

- which causes problems with link\_to and gets pretty messy.

I could just make one content\_controller and have methods like new\_page,  
new\_category etc - but that also seems pretty messy.

This all makes me think that I might be approaching this in the wrong  
way. Ideally i’d like to just write a module and be able to plug it in  
to any application, and not have anything conflict with existing  
tables/classes.

Would i need to look into Rails engines to achieve this? Obviously i’m  
not looking for a detailed breakdown of how to do that but a point in  
the right direction would be great :0)

Cheers,

Steve

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [May 8, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/tables-names-with-model-classes-in-seperate-modules/59925/4 "2006-05-08T21:24:05Z")

</div>

Sorry, i was thinking out aloud with that one - might have seemed a bit  
mixed up :0)

I guess i’m just trying to make sure that doing things the right way in  
terms of using the Rails conventions as much as possible.

Thanks for your advice,

Steve

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [May 8, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/tables-names-with-model-classes-in-seperate-modules/59925/5 "2006-05-08T20:58:51Z")

</div>

On May 8, 2006, at 11:41 AM, Stephen B. wrote:

> Hmm… the only problem with that is that Rails still looks for the  
> model in models/content/page.rb rather than models/content.rb. I’m  
> guessing i could override that but this whole approach is ringing  
> alarm bells…

I’m not sure what you’re trying to achieve.

If you want to plug in “Content” to any app but aren’t sure of what  
you want out of it or what it entails, look into Rails Engines.

If you want to neatly namespace and DRY your content classes, put  
them in a common module and inherit/mixin common behavior. Exploit  
the Rails dir/module nesting conventions as much as possible (look  
for Foo::Bar in foo/bar, requiring foo.rb if present.)

Best,  
jeremy
