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