我很抱歉,但我如何检查保存的关联是否为new_record?因为在保存关联后调用了after_save或before_save。
class Children
belongs_to parent
after_create :check_new_record_of_parent
# before_save :check_parent_saved # call this method also not work
private
def check_new_record_of_parent
parent.new_record? # not work because parent is saved before this method called
end
def check_parent_saved
parent.nil? 0 : 1
end
end
class Parent
has_many children
end
我调用children.update_attributes(nested_form)而不是children.save;
答案 0 :(得分:0)
def before_save
@was_a_new_record = new_record?
return true
end
def after_save
if @was_a_new_record
...
end
end
试试这个,看看它是否有效。