1

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 ? :)

Community
  • 1
  • 1
Dam
  • 231
  • 2
  • 17

1 Answers1

1

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

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

hope you asked for it )

okliv
  • 3,909
  • 30
  • 47
  • Hum, By the way, Now I've three routes going to the same Controller. How is my controller going to know wich one is calling :/ – Dam Jan 28 '13 at 04:31
  • use `action_name` to separate it.. or you can always use session var `session[:current_action]=` during routing, or parsing url... many ways... – okliv Jan 28 '13 at 04:41
  • i overheated with `action_name` =), but `request.path.split("/").include?("trusted_users")` looks like solution – okliv Jan 28 '13 at 05:00