Forum: Ruby on Rails Tables names with model classes in seperate modules

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-08 19:12
(Received via mailing list)
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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-08 19:46
(Received via mailing list)
On May 8, 2006, at 10:10 AM, Stephen Bartholomew 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
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-08 20:43
(Received via mailing list)
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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-08 20:58
(Received via mailing list)
On May 8, 2006, at 11:41 AM, Stephen Bartholomew 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
882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-05-08 21:24
(Received via mailing list)
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
This topic is locked and can not be replied to.