Rails ActiveRecord:即使父属性未更改,accepts_nested_attributes_for 是否将父记录标记为脏记录?
我正在与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果父模型的属性未更改,则该记录不会被视为脏记录,并且
updated_at
不应更改。以此为例(Rails 3.2.3)发布模型
评论模型
Rails控制台测试(我只在粘贴此内容时保留相关输出)
所以在您的情况下一定还有其他东西导致在
事件
。检查是否有任何 回调 可能会执行此操作,并检查您的 < code>belongs_to 方法未使用:touch
选项定义。例如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
Comment model
Rails console test (I only kept relevant output when I pasted this in)
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 yourbelongs_to
method isn't defined with the:touch
option. For example