Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-referential has_many :through with customized :primary key issue

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?

like image 502
porkeypop Avatar asked Jun 25 '10 00:06

porkeypop


1 Answers

I believe it should be like this:

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

while I removed a few things, it looks like your :source => :follow and :source => :subject were switched and it created a circular reference.

like image 114
Geoff Lanotte Avatar answered Sep 24 '22 09:09

Geoff Lanotte