Rails ActiveRecord:即使父属性未更改,accepts_nested_attributes_for 是否将父记录标记为脏记录?

发布于 2024-11-10 15:32:38 字数 564 浏览 0 评论 0原文

我正在与 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

在我的事件表单中,当我不更改任何内容(包括相关链接字段)时,我很好......我的事件模型上没有任何更新。但是,如果我在“RelatedLink”字段中输入 URL,则“event”对象上的“updated_at”就会更新。

UPDATE "events" SET "updated_at" = '2011-05-30 15:27:03.228435' WHERE "events"."id" = 1791

应该这样工作吗?我可以阻止它被标记为脏并被更新吗?

I'm syncing data with an iPhone app, so it's important to know which records have been "actually" updated and which ones have not. I've got an Event model with a RelatedLink association:

In 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

On my event form, when I change nothing, including the RelatedLink field, I'm good ... nothing is updated on my Event model. But if I enter a url in my RelatedLink field, "updated_at" on my Event object is updated.

UPDATE "events" SET "updated_at" = '2011-05-30 15:27:03.228435' WHERE "events"."id" = 1791

Should it work that way? Can I stop it from being marked dirty and being updated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-11-17 15:32:38

如果父模型的属性未更改,则该记录不会被视为脏记录,并且 updated_at 不应更改。以此为例(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

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 

所以在您的情况下一定还有其他东西导致在 事件。检查是否有任何 回调 可能会执行此操作,并检查您的 < code>belongs_to 方法未使用 :touch 选项定义。例如

belongs_to :event, :touch => true

If the parent model's attributes don't change then the record IS NOT considered dirty and the updated_at shouldn't change. Take this for example (Rails 3.2.3)

Post model

class Post < ActiveRecord::Base
  attr_accessible :title, :body, :comments_attributes

  has_many :comments
  accepts_nested_attributes_for :comments
end

Comment model

class Comment < ActiveRecord::Base
  attr_accessible :body, :author
  belongs_to :post
end

Rails console test (I only kept relevant output when I pasted this in)

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 

So there must be something else in your case that is causing an update to be fired on the Event. Check to see if there are any callbacks that might be doing this and also check to see whether your belongs_to method isn't defined with the :touch option. For example

belongs_to :event, :touch => true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文