Я пытаюсь эмулировать модель Твиттера в своих направляющих 2.3.8 приложения (рубиновые 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
Это дает мне "SystemStackError: уровень стека слишком глубоко" ошибка, когда я делаю Пользователя first.linked_from. Причина я должен использовать: user_id вместо стандартного идентификатора - то, потому что мой первичный ключ должен быть строкой.
Что я могу сделать, чтобы заставить отношение работать, так, чтобы я мог сделать Пользователя first.linked_from и Пользователя 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
были переключены, и это создало круговую ссылку.