自我指导has_many:通过自定义:主键问题

我试图在我的Rails 2.3.8应用程序(ruby 1.8.7)中模拟twitter模型

class Connection  '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  '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 

当我执行User.first.linked_from时,这给了我一个“SystemStackError:stack level too deep”错误。 我必须使用的原因:user_id而不是标准id是因为我的主键必须是一个字符串。

我可以做些什么来使关系工作,以便我可以做User.first.linked_from和User.first.linked_to?

我相信它应该是这样的:

 class Connection < ActiveRecord::Base belongs_to :subject, :class_name => 'User' belongs_to :follower, :class_name => 'User' end class User < ActiveRecord::Base set_primary_key "user_id" has_many :relations_to, :foreign_key => 'follower_id', :class_name => 'Connection' has_many :relations_from, :foreign_key => 'subject_id', :class_name => 'Connection' has_many :linked_from, :through => :relations_from, :source => :follow has_many :linked_to, :through => :relations_to, :source => :subject end 

虽然我删除了一些东西,它看起来像你的:source => :follow:source => :subject被切换,它创建了一个循环引用。