开发者

rails relationship using unique key

开发者 https://www.devze.com 2023-01-19 07:12 出处:网络
Table/Entity 1: RetrievedDataRecords Columns: id record_key Table/Entity 2: SourceKeys Columns: id key_name (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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号