我有两张桌子,例如:
table_one n x 1 table_two
但是,table_two有很多table_one元素。
型号:
class TableOne < ActiveRecord::Base
belongs_to :table_two
end
class TableTwo < ActiveRecord::Base
has_many :tables_one, class_name 'TableOne'
end
我想在table_one
更新(在它之前)
table_two
中的所有元素
ps:或者,放置table_one.table_two.delete_all
的正确位置在哪里?
更新1
此命令:
table_two = TableTwo.last
table_two.tables_one = [TableOne.new(<attributes>), TableOne.new(<attributes>, TableOne.new(<attributes>]
table_two.save
应删除table_two的所有现有table_one,然后添加三个新table_one。
答案 0 :(得分:4)
我现在不能尝试,但我想你想要那个:
class TableTwo < ActiveRecord::Base
has_many :table_two, :dependent => :delete_all
end
编辑添加从属标记以避免外键无效而不是破坏记录 资料来源:https://www.ruby-forum.com/topic/4422973