rails relationship between models where primary key not involved
I have 2 models - Batch and Player
Batch has_many :players, :foreign_key => "PL_BATCH"
Player belongs_to 开发者_如何转开发:batch, :foreign_key => "PL_BATCH"
The table batches
contains the following fields:
-BA_OID (defined as the primary key)
-BA_BATCH_ID
-BA_NAME
The table players
contains the field PL_BATCH
which is the foreign key of BA_BATCH_ID
.
Note that I am using a legacy database and the column 'BA_OID ' is defined as the primary key of the batches table in mysql. However BA_BATCH_ID
is used as FK in the players table
Batch model:
set_table_name "batches"
set_primary_key "BA_OID"
has_many :players, :foreign_key => "PL_BATCH"
How do i define the relationship between batch and player models??
Thank you for your help
You're almost there! Try the following...
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"
精彩评论