簡體   English   中英

Rails has_many:在關聯表中使用不同的列名

[英]Rails has_many :through with different column name in association table

我目前有兩種不同的型號: UserProject User模型有三種類型的用戶 - 所有者,承包商和客戶。 我想將多個承包商分配到一個項目。 我正在嘗試使用has_many :through關聯,如下所示:

Class User
   has_many :assignments
   has_many :projects, :through => :assignments 

Class Project
   has_many :assignments
   has_many :contractors, :through => :assignments

Class Assignment
   belongs_to :user
   belongs_to :project

我的問題是在assignments表中使用contractor_id而不是user_id

在我的assignments表中,我目前有列contractor_idproject_id 如果我使用user_id ,一切似乎都有效,但這會導致我的觀點中的事情變得非常混亂。

我怎么做到這一點?

您應該在Assignment使用:foreign_key選項,例如:

class Assignment
  belongs_to :user, :foreign_key => :contractor_id
  belongs_to :project

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM