Quick and easy has_many routes question

Hey,

i have- map.resources :courses, :has_many => :pages

now what if i wanted for courses to has_many => :forums as well?

can i just do map.resources :courses, :has_many => :pages, :forums

?? because that doesn’t work. whats the syntax for- has many blank AND
blank…

lol reall noobie i know, but i just don’t know how to do it.

Have you tried?

map.resources :courses, :has_many => [:pages, :forums]

oh the brackets. thanks guys. :slight_smile:

i have- map.resources :courses, :has_many => :pages

now what if i wanted for courses to has_many => :forums as well?

can i just do map.resources :courses, :has_many => :pages, :forums

Nope, it would be:

map.resources :courses, :has_many => [:pages, :forums]

You map it to an array. In Ruby any hash style assignments in a method
call, which this is, but must come at the end to be implicitly converted
to
a hash. In the initial example you are doing this:

map.resources(:courses, {:has_many => :pages})

And that works just as well as the normal version, but it’s not as
readable.
If you did it your way, Ruby wouldn’t be able to make the last
arguments a
hash as you have just a key as the last argument (your :forums looks
like an
additional argument to map.resources not a part of :has_many).

Anyway, answered in the first line of code, hopefully the rest may make
it
slightly clearer.

Cheers,

Andy

oh yeah then what if i wanted–

a has many b, and b has many c, and c has many d

how would i do that in a route? like multiple stacked layers, how
would i write that in my routes.rb?

when I have more than one nested ‘has_many’ resource, i think about
creating a namespace…
http://seanbehan.com/ruby-on-rails/nesting-resources-in-rails-routes-rb-with-namespaces/
…this keeps your other controllers (forums, pages) clean and restful