rails relationship using unique key
Table/Entity 1: RetrievedDataRecords
Columns:
id
record_key
Table/Entity 2: SourceKeys
Columns:
id
key_name (unique key)
I cant alter these tables for legacy reasons, but want rails relationship between RetrievedDataRecord and SourceKey using record_key and key_name (they are the same key)
in RetrievedDataRecord, I tried something like:
belongs_to :source_key, :foreign_key => :record_key
but开发者_Go百科 this doesn't work. I need some way to tell rails that record_key on this table is the same as key_name on the other table....
You have to specify also the primary_key
:
belongs_to :source_key, :primary_key => :key_name, :foreign_key => :record_key
It should work!
精彩评论