- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我做了一个真正的基础github项目here这说明了这个问题。基本上,当我创建一个新评论时,它会按预期保存;当我更新现有评论时,它不会被保存。然而,这不是 :autosave => true
的文档所说的……他们说的恰恰相反。这是代码:
class Post < ActiveRecord::Base
has_many :comments,
:autosave => true,
:inverse_of => :post,
:dependent => :destroy
def comment=(val)
obj=comments.find_or_initialize_by(:posted_at=>Date.today)
obj.text=val
end
end
class Comment < ActiveRecord::Base
belongs_to :post, :inverse_of=>:comments
end
现在在控制台中,我测试:
p=Post.create(:name=>'How to groom your unicorn')
p.comment="That's cool!"
p.save!
p.comments # returns value as expected. Now we try the update case ...
p.comment="But how to you polish the rainbow?"
p.save!
p.comments # oops ... it wasn't updated
为什么不呢?我错过了什么?
请注意,如果您不使用“find_or_initialize”,它会像 ActiveRecord 尊重关联缓存一样工作 - 否则它会过于频繁地重新加载注释,从而丢弃更改。即,此实现有效
def comment=(val)
obj=comments.detect {|obj| obj.posted_at==Date.today}
obj = comments.build(:posted_at=>Date.today) if(obj.nil?)
obj.text=val
end
但是,当然,如果我可以只使用数据库,我不想遍历内存中的集合。另外,它适用于新对象而不适用于现有对象,这似乎不一致。
最佳答案
这是另一种选择。如果不是新记录,您可以将 find_or_initialize_by
返回的记录显式添加到集合中。
def comment=(val)
obj=comments.find_or_initialize_by(:posted_at=>Date.today)
unless obj.new_record?
association(:comments).add_to_target(obj)
end
obj.text=val
end
关于ruby-on-rails - rails 协会 :autosave doesn't seem to working as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455173/
对于在发送电子邮件之前自动保存电子邮件或在完成或正式保存之前保存博客文章的应用程序,最佳策略是什么?最好在数据库中使用单独的表作为临时草稿,或者使用状态列将帖子标记为草稿或已发布?我不是在寻找代码,只
我习惯在 TextPad 中打开很多文本窗口,大约 20 个或更多,都没有保存。 几天后,我浏览了它们并保存了我需要/想要的。 这工作正常,除了当我的电脑崩溃时……当我播放视频时它似乎崩溃了,但只是有
我正在与Eclipse(Indigo)合作开发C项目。当我在新的工作区中创建新的C项目时,默认情况下未设置“生成前自动保存”选项。这非常令人沮丧,因为自动保存通常是我开发的其他IDE的默认行为。我更改
我的应用程序的表单为 成分。一旦表单值发生更改,该组件就会调用提交。一切正常,但是当更改路线时,它会更改表单值和 调用提交。如何解决这个问题呢?一个可能的解决方案是安装 改变路线时再次。 Codes
我如何在 C# 中实现自动保存?我觉得保存到当前打开的文件很简单,但我可能不想覆盖我以前的文件(或者我应该这样做吗?我认为谷歌文档保存/覆盖文档,他们有版本控制?)。我想保存到另一个文件,但我应该保存
是否可以在 Xcode 8 中关闭编辑的自动保存? 我看过一篇从终端运行 defaults write com.apple.dt.Xcode ApplePersistence -bool no 的帖子
文档只是指出,在共享文档 Controller 上将 setAuto savingDelay 设置为大于 0 的值就可以做到这一点,但是在调用之后 [[NSDocumentController sha
我在 CentOS 5.6 上使用 Eclipse Helios,并且(可能)在(或者可能是由于?)为 Jetty 下载了一个插件之后,IDE 在构建之前停止了自动保存。中的相应选项 Window -
我正在尝试构建一个 Rails 应用程序,您可以在其中从页面上的多个不同位置编辑相同的模型。我希望每 X 秒自动保存一次更改,并可以选择手动触发保存。 我想出了一个解决方案,但它看起来很复杂,而且我假
我的网站文件存储在 c waamp www 文件夹中,我使用 devtools autosave 更新我的 css 文件。但是在本地主机上工作时它不起作用。我需要做什么才能让它与本地主机一起工作? 最
我有一个使用 CoreData 的基于文档的 Cocoa 应用程序,它可以导入大量数据。由于此导入需要一些时间,所以我在后台进行,但由于 NSManagedObjectContext 不是线程保存,所
我刚刚学习了 Visual Studio Code,对设置的实际情况并不是很熟悉。 我一直试图从 中排除文件类型自动保存 但我无法让它发挥作用。 自动保存有效,但定义排除的部分不起作用(基本上假设从自
我有两个模型 Board has_one :pref, :autosave => true, :dependent => :destroy Pref belongs_to :board pre
这可能是一件非常简单的事情,但我一辈子都做不到。出于某种原因,:autosave 实际上并不是自动保存底层模型。 这是我的架构: create_table :albums do |t| t
我做了一个真正的基础github项目here这说明了这个问题。基本上,当我创建一个新评论时,它会按预期保存;当我更新现有评论时,它不会被保存。然而,这不是 :autosave => true 的文档所
在 Rails 2 和 Rails 3 中,如果 :autosave => true 是一个 has_many 关联,则循环遍历集合并在每个子关联上调用 save(:validate => false
我使用的是 Rails 4.2.3 在我的数据库中,我有一个“my_objects”表,其中有一个“address_id”,它是返回“addresses”表的外键。我的 app/models/my_o
我有一个用户模型,其中有一个图片。我怎样才能禁用自动保存选项。当我尝试以下操作时出现错误。我正在使用 Rails 3。 class User "ImageMapping",:as => :imagea
Chrome DevTools Autosave不适用于 Rails Asset Pipeline .问题的罪魁祸首是 Assets URL——我无法通过 URL 破译实际的文件路径。例如,/asse
在 Ruby on Rails 中,如果你声明 accepts_nested_attributes_for 在您的模型中,autosave对于子关联设置为 true。这是必要的吗? 根据我的理解,Ra
我是一名优秀的程序员,十分优秀!