REST routing issue w self-referential resource join model

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 =>

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

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… ;-))


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs