如果父模型的属性不会再更改记录不被认为是肮脏和updated_at
不应该改变。借此例如(Rails的3.2.3)
Post模型
class Post < ActiveRecord::Base
attr_accessible :title, :body, :comments_attributes
has_many :comments
accepts_nested_attributes_for :comments
end
评价模型
class Comment < ActiveRecord::Base
attr_accessible :body, :author
belongs_to :post
end
Rails的控制台测试(我只保留相关的输出,当我将这些中)
1.9.3p194 :001 > p = Post.create :title => "Nested attributes", :body => "Nested attributes are awesome", :comments_attributes => [{ :body => "I agree", :author => "Bart" }]
=> #<Post id: 1, title: "Nested attributes", body: "Nested attributes are awesome", created_at: "2012-06-13 01:49:34", updated_at: "2012-06-13 01:49:34">
1.9.3p194 :002 > last_post = Post.last
=> #<Post id: 1, title: "Nested attributes", body: "Nested attributes are awesome", created_at: "2012-06-13 01:49:34", updated_at: "2012-06-13 01:49:34">
1.9.3p194 :003 > last_post.comments_attributes = [{:id => 1, :author => "Bort"}]
=> [{:id=>1, :author=>"Bort"}]
1.9.3p194 :004 > last_post.changed?
=> false
1.9.3p194 :005 > last_post.save
(0.1ms) begin transaction
(0.7ms) UPDATE "comments" SET "author" = 'Bort', "updated_at" = '2012-06-13 01:51:05.032862' WHERE "comments"."id" = 1
(250.1ms) commit transaction
=> true
1.9.3p194 :006 > last_post.updated_at
=> Wed, 13 Jun 2012 01:49:34 UTC +00:00
所以一定有什么东西在其他情况下,是造成一个更新的被解雇。请检查是否有可能这样做的,也检查,看看是否你的belongs_to
方法不与:touch
选项定义的任何callbacks。例如
belongs_to :event, :touch => true