自连接的正确 ActiveRecord 关联 类
Proper ActiveRecord associations for self-joined classes
我一直在修改在线 Rails 教程以满足我的特定需求,但我发现自己力不从心。一点点清晰度将不胜感激。我的代码在我的问题下面。
此时,如果dan
和rob
是classUser
的实例,我调用
dan.add_supervisor(rob)
然后 returns 一个空集合:
dan.supervisors
=> #<ActiveRecord::Associations::CollectionProxy []>
然而监管本身已经创建:
dan.initiated_supervisions
=> #<ActiveRecord::Associations::CollectionProxy [#<Supervision id: 50, supervisor_id: 68, supervisee_id: 67, created_at: "2015-03-10 18:30:01", updated_at: "2015-03-10 18:30:01">]>
理想的行为是让 has_many :through
行为正常工作,并且能够通过调用 dan.supervisors
获得 Dan 的主管列表。我需要做出哪些改变才能实现这一目标?
User.rb
...
# Supervisees can choose their supervisors,
# but not the other way around.
has_many :initiated_supervisions, class_name: "Supervision",
foreign_key: "supervisee_id",
dependent: :destroy
has_many :non_initiated_supervisions, class_name: "Supervision",
foreign_key: "supervisor_id",
dependent: :destroy
has_many :supervisees, through: :non_initiated_supervisions
has_many :supervisors, through: :initiated_supervisions
...
def add_supervisor(other_user)
initiated_supervisions.create(supervisor_id: other_user.id)
end
...
Supervision.rb
...
belongs_to :supervisor, class_name: "User"
belongs_to :supervisee, class_name: "User"
validates :supervisor_id, presence: true
validates :supervisee_id, presence: true
...
解决方案(暂时...拜托,哦,请继续工作...)在两个 User
模型关联中指定 class_name: "User"
:
has_many :supervisors, through: :initiated_supervisions, class_name: "User"
has_many :supervisees, through: :non_initiated_supervisions, class_name: "User"
我一直在修改在线 Rails 教程以满足我的特定需求,但我发现自己力不从心。一点点清晰度将不胜感激。我的代码在我的问题下面。
此时,如果dan
和rob
是classUser
的实例,我调用
dan.add_supervisor(rob)
然后 returns 一个空集合:
dan.supervisors
=> #<ActiveRecord::Associations::CollectionProxy []>
然而监管本身已经创建:
dan.initiated_supervisions
=> #<ActiveRecord::Associations::CollectionProxy [#<Supervision id: 50, supervisor_id: 68, supervisee_id: 67, created_at: "2015-03-10 18:30:01", updated_at: "2015-03-10 18:30:01">]>
理想的行为是让 has_many :through
行为正常工作,并且能够通过调用 dan.supervisors
获得 Dan 的主管列表。我需要做出哪些改变才能实现这一目标?
User.rb
...
# Supervisees can choose their supervisors,
# but not the other way around.
has_many :initiated_supervisions, class_name: "Supervision",
foreign_key: "supervisee_id",
dependent: :destroy
has_many :non_initiated_supervisions, class_name: "Supervision",
foreign_key: "supervisor_id",
dependent: :destroy
has_many :supervisees, through: :non_initiated_supervisions
has_many :supervisors, through: :initiated_supervisions
...
def add_supervisor(other_user)
initiated_supervisions.create(supervisor_id: other_user.id)
end
...
Supervision.rb
...
belongs_to :supervisor, class_name: "User"
belongs_to :supervisee, class_name: "User"
validates :supervisor_id, presence: true
validates :supervisee_id, presence: true
...
解决方案(暂时...拜托,哦,请继续工作...)在两个 User
模型关联中指定 class_name: "User"
:
has_many :supervisors, through: :initiated_supervisions, class_name: "User"
has_many :supervisees, through: :non_initiated_supervisions, class_name: "User"