我有2个模型 - 批量和播放器
Batch has_many :players, :foreign_key => "PL_BATCH"
Player belongs_to :batch, :foreign_key => "PL_BATCH"
表batches
包含以下字段:
-BA_OID (defined as the primary key)
-BA_BATCH_ID
-BA_NAME
表players
包含字段PL_BATCH
,它是BA_BATCH_ID
的外键。
请注意,我使用的是旧数据库,并且“BA_OID”列被定义为mysql中批处理表的主键。但是BA_BATCH_ID
在播放器表格中用作FK
批量模型:
set_table_name "batches"
set_primary_key "BA_OID"
has_many :players, :foreign_key => "PL_BATCH"
如何定义批次和玩家模型之间的关系?
感谢您的帮助
答案 0 :(得分:2)
你快到了!请尝试以下方法......
Batch
:
has_many :players, :primary_key => "BA_BATCH_ID", :foreign_key => "PL_BATCH", :class_name => "Player"
Player
:
belongs_to :batch, :primary_key => "BA_BATCH_ID", :foreign_key => "PL_BATCH", :class_name => "Batch"