Dam
Dam

Reputation: 231

Rails 3 - Nested Routes for has_many through self-referential following/follower relationships

I made such a relationship using this answer: Ruby on Rails has_many through self-referential following/follower relationships

So I've the following Models :

class User < ActiveRecord::Base

  has_many :is_trustings
  has_many :trusted_users, :through => :is_trustings, :source => 'trusted'

  has_many :trusters, :class_name => 'IsTrusting', :foreign_key => 'trusted_id'
  has_many :trusting_users, :through => :trusters, :source => :user

end

&

class IsTrusting < ActiveRecord::Base
  belongs_to :user
  belongs_to :trusted, :class_name => 'User'
end

Everything is okay when using it in console. Now I would like to make a nested route to allow a client to retrieve an user's followers.

But It don't work the way I expect...

See, in my routes.rb I wrote

resources :user do
   resources :is_trusting
   resources :trusted_users
   resources :trusting_users
end

Wich makes routes like this :

/users/:user_id/is_trustings(.:format)       is_trusting#index

I'm okay with this one as it's the intermediary model

Now I want the embeded modelsn but here is what I get :

/users/:user_id/trusted_users(.:format)       trusted_users#index
/users/:user_id/trusting_users(.:format)      trusting_users#index

This obviously don't work as there is no TrustedUsersController. I wan't this routes to go for UserController.

There must be a built-in shortcut like the :class_name => 'Users' in Models...

Does somebody know it ? :)

Upvotes: 1

Views: 1058

Answers (1)

okliv
okliv

Reputation: 3959

as far as i understood the question you can just use :controller key

#....
 resources :trusted_users, :controller => :users
#..

hope you asked for it )

Upvotes: 1

Related Questions