如何从两个不同的连接表中获取数据

时间:2016-08-07 11:04:49

标签: ruby-on-rails

我有两个加入表第一个在alarm_id | list_id和另一个之间list_id | car_id要做什么以进入报警节目,每个列表中的汽车,我试图使用has_many:cars:通过=>列表,但它不起作用 任何帮助,请

1 个答案:

答案 0 :(得分:1)

您必须正确设置关联。根据你的要求,它应该看起来像这样。

class Alarm < ActiveRecord::Base
  has_many :alarm_lists
  has_many :lists, through: :alarm_lists
  has_many :car_lists, through: :lists
  has_many :cars, through: :car_lists
  ...
end

class List < ActiveRecord::Base
  has_many :car_lists
  has_many :cars, through: :car_lists
  ...
end

class Car < ActiveRecord::Base
  has_many :car_lists
  has_many :lists, through: :car_lists
  ...
end

class CarList < ActiveRecord::Base
  belongs_to :car
  belongs_to :list
  ...
end

class AlarmList < ActiveRecord::Base
  belongs_to :alarm
  belongs_to :list
  ...
end

有关详细信息,请查看here