Ruby on rails Rails ActiveRecord:是否接受\u嵌套的\u属性\u以将父记录标记为脏记录,即使父属性不变?
我正在用iPhone应用程序同步数据,所以知道哪些记录“实际”更新了,哪些没有更新很重要。我有一个具有关联链接关联的事件模型: 在event.rb中:Ruby on rails Rails ActiveRecord:是否接受\u嵌套的\u属性\u以将父记录标记为脏记录,即使父属性不变?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我正在用iPhone应用程序同步数据,所以知道哪些记录“实际”更新了,哪些没有更新很重要。我有一个具有关联链接关联的事件模型: 在event.rb中: has_many :related_links accepts_nested_attributes_for :related_links, :reject_if => lambda { |a| a[:url].blank? && a[:id].blank? }, :allow_destroy => true 在我的事
has_many :related_links
accepts_nested_attributes_for :related_links, :reject_if => lambda { |a| a[:url].blank? && a[:id].blank? }, :allow_destroy => true
在我的事件表单上,当我什么都不更改时,包括RelatedLink字段,我很好。。。我的事件模型上没有更新任何内容。但如果我在我的RelatedLink字段中输入一个url,我的事件对象上的“updated_at”就会被更新
UPDATE "events" SET "updated_at" = '2011-05-30 15:27:03.228435' WHERE "events"."id" = 1791
这样行吗?我可以阻止它被标记为脏并被更新吗?如果父模型的属性没有更改,则记录不被认为是脏的,并且在处更新的记录不应该更改。以此为例(Rails 3.2.3) 后模型
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
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 :event, :touch => true