是否有可能知道相关对象何时被销毁?

时间:2010-11-18 14:33:52

标签: ruby-on-rails ruby-on-rails-3

如果你在Rails 3中有一对多关联并且通过删除接受嵌套属性,是否可以通过查看对象(关联对象)来知道它是否会被删除?

例如:

group.attributes = {:member_attributes => {"0" => {:id => 1, :name => "John"},
                                           "1" => {:id => 2, :name => "Dave"},
                                           "2" => {:id => 3, :name => "Gus", "_destroy" => true}}}

通过查看group.members是否可以知道在保存时将删除id为3的那个?

1 个答案:

答案 0 :(得分:5)

有一种找出out的方法,叫做marked_for_destruction?

group.members.each do |member|
    puts "#{member.name} => #{member.marked_for_destruction?}"
end

会生成

John false
Dave false
Gus true