rails模型之间的关系,其中主键不涉及

时间:2011-06-06 18:57:26

标签: ruby-on-rails

我有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"

如何定义批次和玩家模型之间的关系?

感谢您的帮助

1 个答案:

答案 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"