I'm trying to emulate the twitter model in my Rails 2.3.8 app (ruby 1.8.7)
class Connection < ActiveRecord::Base
belongs_to :subject, :foreign_key => 'subject_id', :primary_key => 'user_id', :class_name => 'User'
belongs_to :follower, :foreign_key => 'follower_id', :primary_key => 'user_id', :class_name => 'User'
end
class User < ActiveRecord::Base
has_many :relations_to, :primary_key => 'user_id', :foreign_key => 'follower_id', :class_name => 'Connection'
has_many :relations_from, :primary_key => 'user_id', :foreign_key => 'subject_id', :class_name => 'Connection'
has_many :linked_from, :through => :relations_from, :source => :subject, :primary_key => 'user_id'
has_many :linked_to, :through => :relations_to, :source => :follower, :primary_key => 'user_id'
end
This gives me a "SystemStackError: stack level too deep" error when I do User.first.linked_from. The reason I have to use :user_id instead of the standard id is because my primary key has to be a string.
What can I do to make the relation work, so that I can do User.first.linked_from and User.first.linked_to?