Tables names with model classes in seperate modules


#1

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


#2

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


#3

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


#4

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


#5

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