:autosave 忽略了 has_many 关系——我错过了什么? [英] :autosave ignored on has_many relation -- what am I missing?

查看:31
本文介绍了:autosave 忽略了 has_many 关系——我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对班级:

class Collection < ActiveRecord::Base                                                                                                                                                                                                         
  has_many :items, autosave: true
end

class Item < ActiveRecord::Base
  belongs_to :collection
end

来自文档:

当 :autosave 为真时,所有子项都会被保存,无论它们是否是新记录:

When :autosave is true all children is saved, no matter whether they are new records:

但是当我更新 Item 并保存其父 Collection 时,Item 的更新属性不会被保存:

But when I update an Item and save its parent Collection, the Item's upated attributes don't get saved:

 > 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, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]

那么,我错过了什么?

我使用的是 Rails 3.2.5 和 Ruby 1.9.2.

I'm using Rails 3.2.5 and Ruby 1.9.2.

所以我在 ActiveRecord 的源代码中做了一些挖掘.我们可以得到 c 的自动保存关联:

So I've done some digging about in the source of ActiveRecord. We can get hold of c's autosave assocations:

 > 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: datetime, updated_at: datetime), @plural_name="items", @collection=true, @class_name="Item", @klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), @foreign_key="collection_id", @active_record_primary_key="id", @type=nil>]

我认为这说明关联已设置为自动保存.

I think this illustrates that the association has been set up for autosaving.

然后我们就可以得到c对应的关联实例:

We can then get the instance of the association corresponding to c:

 > a = c.send :association_instance_get, :items
 => #<ActiveRecord::Associations::HasManyAssociation:0x007fece738e920 @target=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], @reflection=#<ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 @macro=:has_many, @name=:items, @options={:autosave=>true, :extend=>[]}, @active_record=Collection(id: integer, name: string, created_at: datetime, updated_at: datetime), @plural_name="items", @collection=true, @class_name="Item", @klass=Item(id: integer, collection_id: integer, name: string, created_at: datetime, updated_at: datetime), @foreign_key="collection_id", @active_record_primary_key="id", @type=nil>, @owner=#<Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10">, @updated=false, @loaded=true, @association_scope=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], @proxy=[#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">], @stale_state=nil> 

然后我们可以找到通过这个关联关联的实际对象:

We can then find the actual objects that are associated via this association:

 > a.target
 => [#<Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25">]

此处找到的对象没有我之前所做的更新.

The object found here does not have update that I'd made earlier.

推荐答案

问题出在这里

 i = c.items.first

这一行从数据库中提取正确的项目,但没有将其附加到集合 c.它是与对象不同的 ruby​​ 对象

This line pulls the correct item from the database, but doesn't attach it to the collection c. It is a distinct ruby object from the object

i = c.items[0]

如果您用第二行替换第一行,您的示例将起作用.

If you replace the first line with the second your example will work.

这篇关于:autosave 忽略了 has_many 关系——我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆