大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – :在has_many关系中忽略自动保存 – 我缺少什么?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class Collection < ActiveRecord::Base has_many :items,autosave: true end class Item < ActiveRecord::Base belongs_to :collection end@H_197_4@从docs: @H_197_4@但是当我更新一个项目并保存其父集合时,该项目的upated属性不能被保存:
> c = Collection.first => #<Collection id: 1,name: "collection",created_at: "2012-07-23 00:00:10",updated_at: "2012-07-23 00:00:10"> > i = c.items.first => #<Item id: 1,collection_id: 1,name: "item1",created_at: "2012-07-23 00:00:25",updated_at: "2012-07-23 00:00:25"> > i.name = 'new name' => "new name" > c.save => true > Collection.first.items => [#<Item id: 1,updated_at: "2012-07-23 00:00:25">]@H_197_4@那么,我失踪了什么? @H_197_4@我使用的是Rails 3.2.5和Ruby 1.9.2. @H_197_4@所以我在ActiveRecord的源头做了一些挖掘.我们可以掌握c的自动保存协议:
> c.class.reflect_on_all_autosave_associations => [#<ActiveRecord::reflection::AssociationReflection:0x007fece57b3bd8 @macro=:has_many,@name=:items,@options={:autosave=>true,:extend=>[]},@active_record=Collection(id: Integer,name: String,created_at: datetiR_138_11845@e,updated_at: datetiR_138_11845@E),@plural_name="items",@collection=true,@class_name="Item",@klass=Item(id: Integer,collection_id: Integer,@foreign_key="collection_id",@active_record_priMary_key="id",@type=nil>]@H_197_4@我认为这表明该协会是为了自动保存设立的. @H_197_4@然后我们可以得到对应于c的关联实例:
> a = c.send :association_instance_get,:items => #<ActiveRecord::Associations::HasmanyAssociation:0x007fece738e920 @target=[#<Item id: 1,updated_at: "2012-07-23 00:00:25">],@reflection=#<ActiveRecord::reflection::AssociationReflection:0x007fece57b3bd8 @macro=:has_many,@type=nil>,@owner=#<Collection id: 1,updated_at: "2012-07-23 00:00:10">,@updated=false,@loaded=true,@association_scope=[#<Item id: 1,@proxy=[#<Item id: 1,@stale_state=nil>@H_197_4@然后,我们可以找到通过此关联关联的实际对象:
> a.target => [#<Item id: 1,updated_at: "2012-07-23 00:00:25">]@H_197_4@这里找到的对象没有我之前提供的更新.
以上是大佬教程为你收集整理的ruby-on-rails – :在has_many关系中忽略自动保存 – 我缺少什么?全部内容,希望文章能够帮你解决ruby-on-rails – :在has_many关系中忽略自动保存 – 我缺少什么?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。