我有以下问题。我有模型bus_routes和break_points。 BusRoute有2个breakPoints 我创建了以下关系
class BusRoute < ActiveRecord::Base
attr_accessible :destination_id, :origen_id
belongs_to :destination, class_name: "BreakPoint"
belongs_to :origen, class_name: "BreakPoint"
end
class BreakPoint < ActiveRecord::Base
attr_accessible :city,:province_id,:province
end
如果我在BusRoute destination_id或origen_id中删除BreakPoint任何记录,我需要这样做 具有breakPoint消除的id被删除的数据库具有依赖:: destroy
答案 0 :(得分:1)
以下内容将起作用:
class BreakPoint < ActiveRecord::Base
has_many :destinations,
foreign_key: :destination_id,
class_name: 'BusRoute',
dependent: :destroy
has_many :origens,
foreign_key: :origen_id,
class_name: 'BusRoute',
dependent: :destroy
end
答案 1 :(得分:0)
BusRoute中的任何记录都不能删除breakPoint的id Becouse BusRoute有BreakPoint,但BreakPoint没有BusRoute 在BreakPoint中必须是bus_route_id BusRoute没有break_point_id。