两次belongs_to映射回同一个表'用户'

时间:2014-04-07 04:00:05

标签: ruby-on-rails ruby

class Comment < ActiveRecord::Base
   belongs_to :commenter, :class_name => "User", :foreign_key => "commenter_user_id"
   belongs_to :student, :class_name => "User", :foreign_key => "student_user_id"

他们都属于用户。

如果这样做了一次,我会得到它,因为它有助于使它更清晰或什么......

但为什么要两次将它映射到同一张桌子?!?!?

因此,结果评论栏是这样的:

  [0] "id",
  [1] "comment",
  [2] "student_user_id",
  [3] "commenter_user_id",
  [4] "created_at",
  [5] "updated_at",
  [6] "partnership_id"

如果它与我之前的问题相同,请原谅。对我来说,事实并非如此。或者也许我说得不够清楚。嗯。

1 个答案:

答案 0 :(得分:1)

Comment可以Commentator发布User Comment Student也可以User发布class Comment < ActiveRecord::Base belongs_to :commenter, :class_name => "User", :foreign_key => "commenter_user_id" belongs_to :student, :class_name => "User", :foreign_key => "student_user_id" class Comment < ActiveRecord::Base belongs_to :commenter belongs_to :student {1}}所以最好像你的方式一样定义

{{1}}

而不仅仅是像这样定义

{{1}}