Topic: Namespaces and nested resources

Hello!

I'm developing online game (Heroes fighting against Clans) and i'm planning next structure of my app: i've got top-level resources Heroes (profiles of supermembers) and Clans (profiles of groups of members). So i've got both HeroesController and ClansController.

What i wanna do is give Heroes and Clans' leaders opportunities to add "Content Resources" to their profile area or to the clan  area, respectively. "Content Resources" is different singular resources such as Gallery, Blog, Shop and others. So Gallery has many Photoalbums (that have many Photos), Blog has many Posts, Shop has goods and so on.

Hero can turn on/off each of singular Content Resources, and after that he get its full functionallity on his own profile page
The same thing with Clans - clan leader can connect in settings area for example Forum.

So the question is how to organize this structure well. I think about that way:

/controllers
    HeroesController
    ClansController
    /blog/
        Blog::BlogsController
        Blog::PostsController
    /gallery/
        Gallery::GalleriesController
        Gallery::PhotoalbumsController
        Gallery::PhotosController
/models
    Hero
    Clan
    module Blog
    /blog/
        Blog::Blog
        Blog::Post
        module Gallery
    /gallery/
        Gallery::Gallery
        Gallery::Photoalbums
        Gallery::Photos
       
And routes:

resources :heroes
resources :clans

namespace :blog do
  resources :blogs do
    resources :topics
  end
end

namespace :gallery do
  resources :galleries do
    resources :photoalbums
    resources :photos
  end

  resources :photoalbums
    resources :photos
  resoursces
end

And here i've got a first problem - how to attach Gallery to Hero and/or Blog to Clan?

May be i'm doing everything wrong. May be i should choose another way.

I'm new in Rails, and i'm sorry for maybe easy questions smile

Grateful for any help!
Thank you!

Last edited by Ajaxy (2011-08-04 13:42:20)

Re: Namespaces and nested resources

From every rails app I've seen, all models are left in the root of the models directory. If this strikes you as unorganized, you may be able to get away with the following

#in hero.rb or any other model you want to give a gallery to
has_many :galleries, :as => :owner, :class_name => 'Gallery::Gallery'

This is assuming that the galleries table is polymorphic and has an owner_id and owner_type (owner type in this case would be 'Hero'). Would you mind posting the schema and/or source code so that I can elaborate further?
Thijs

Re: Namespaces and nested resources

Oh, and assuming you already know about how to do rails relationships on non namespaced models, essentially if your going against the grain with naming convention, you need to use

:class_name => 'NameOfClass' in cases where you want to define a relationship other than :name_of_class.