本文介绍了导轨定制foreign_key两个表上的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个型号,例如用户和俱乐部以及它们的属性:

I have two models, for example User and Club with their attributes:

User:
  id
  uid
  email
  etc.

Club:
  id
  player_id
  address
  supporter
  etc.

由于某些原因,连接属性是 clubs.player_id users.uid 不是 clubs.player_id users.id 。是否有可能连接这两个模型一到一使用联想 HAS_ONE belongs_to的? THX

For some reason, the join attribute is clubs.player_id with users.uid NOT clubs.player_id with users.id. Is it possible connecting these two model with one-to-one association using has_one and belongs_to? thx

推荐答案

我敢打赌,这将工作:

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

这篇关于导轨定制foreign_key两个表上的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:54