Rails自定义foreign_key名称在两个表上

ray*_*ibi 6 ruby-on-rails foreign-keys associations

我有两个模型,例如User和Club及其属性:

User:
  id
  uid
  email
  etc.
Run Code Online (Sandbox Code Playgroud)

Club:
  id
  player_id
  address
  supporter
  etc.
Run Code Online (Sandbox Code Playgroud)

出于某种原因,join属性clubs.player_id使用users.uidNOT clubs.player_idwith users.id.是否可以将这两个模型与one-to-one关联使用has_onebelongs_to?谢谢

Ada*_*dam 22

我打赌这会奏效:

class User < ActiveRecord::Base
  has_one :club, :foreign_key => :player_id, :primary_key => :uid
end

class Club < ActiveRecord::Base
  belongs_to :user, :foreign_key => :player_id, :primary_key => :uid
end
Run Code Online (Sandbox Code Playgroud)