如何将 belongs_to 添加到两个不同列中的相同 table?

How to add belongs_to to the same table in two different columns?

我有一个用户 table 和一个任务 table,我想要一个任务 table 的模式,其中有 from_id 和 to_id 之类的。

belongs_to(:user, TaskTracker2.Accounts.User, foreign_key: :from_id)
belongs_to(:user, TaskTracker2.Accounts.User, foreign_key: :to_id)

但它引发了参数错误,如何以正确的方式进行? 谢谢。

您需要在第一个参数中使用唯一值。像

belongs_to(:user_from, TaskTracker2.Accounts.User, foreign_key: :from_id)
belongs_to(:user_to, TaskTracker2.Accounts.User, foreign_key: :to_id)