views:

115

answers:

1

I have an aplication that has an User and a Project model. I have the following relationships between those 2 models:

USER
has_many :authorships , :foreign_key => :author_id
has_many :moderatorships, :foreign_key => :moderator_id
has_many :authored_projects, :through => :authorships, :class_name => 'Project'
has_many :moderated_projects, :through => :moderatorships, :class_name => 'Project'

I want to have a route that is /users/id/favorite_projects and /users/id/moderated_projects. I have the following in my routes.rb

map.resources :users,:has_many => [:authored_projects, :moderatored_projects], :shallow => true, :collection => {:logins => :get}

But when I run rake routes I get

user_authored_projects GET    /users/:user_id/authored_projects(.:format)        {:controller=>"authored_projects", :action=>"index"}

But, I don't really have an authored_projects controller. How can I achieve that?

A: 

I think you want to do this instead in your routes:

map.resources :users do |users|
  users.resources :authored_projects, :controller => :projects
  users.resources :moderated_projects, :controller => :projects
end

But I think you want to do something else, like this:

map.resources :users, :member => [:authored_projects, :moderated_projects]

And then create the actions authored_projects and moderated_projects in your UsersController.

dvyjones
Not actually. I don't really want to create the other controller. I already have the Projects controller and in the model relationship I specify that "authored_projects" are actually projects. I want to be able to specify that in the :has_many => [] option in the routes.
Hock