Forum: Ruby on Rails REST routing issue w self-referential resource join model..

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kad K. (Guest)
on 2007-05-12 12:49
I have a 'user' resource  w route =>    map.resources :users

user model is defined as self-referential
class User < ActiveRecord::Base
  has_many :relationships_as_befriender, :foreign_key =>
'befriender_id', :class_name => 'User'
  has_many :relationships_as_friend,   :foreign_key => 'friend_id',
:class_name => 'User'
  has_many :friends,  :through => :relationships_as_friend
  has_many :befrienders,  :through => :relationships_as_befriender
....

I created the nested 'relationship' resource

class Relationship < ActiveRecord::Base
  set_primary_key :relation_id
  belongs_to :befriender, :foreign_key => "befriender_id", :class_name
=> "User"
  belongs_to :friend,   :foreign_key => "friend_id",   :class_name =>
"User"
...

if I had a one-to-many , I know that the route should be created like
that...
  map.resources :users do |users|
    users.resources :relationships
  end

but if I require    /users/1/relationships   ,  it will use the user_id
as a foreign_key... and fail
relationship_url failed to generate from {:action=>"show",
:user_id=>"1", :controller=>"relationships"}, expected:
{:action=>"show", :controller=>"relationships"}, diff: {:user_id=>"1"}

how should I write teh routes ?  (tips or links to solution
accepted..... ;-))

kad
This topic is locked and can not be replied to.