引用相同模型的Rails活动记录关联

时间:2018-11-27 02:13:06

标签: ruby-on-rails ruby

我正在寻求一些帮助来建立这两个表之间的activerecord关联。

尤其是与政党ID和反对党ID的关联-我想都指向党派表。

有人可以协助我在模型中进行设置吗?

Tables

Election
Id:integer
Location_id:integer
Date:date
Government_party_id:integer
Government_seats:integer
Government_tpp:float
Opposition_party_id:integer
Opposition_seats:integer
opposition_tpp:float
Total_seats:integer

Party
Id:integer
Name:string

1 个答案:

答案 0 :(得分:0)

class Election < ActiveRecord::Base
  belongs_to :government_party, class_name: 'Party', foreign_key: 'Government_party_id'
  belongs_to :opposition_party, class_name: 'Party', foreign_key: 'Opposition_party_id'
end