11

我有一对类:

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

class Item < ActiveRecord::Base
  belongs_to :collection
end

文档

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

但是当我更新 anItem并保存它的 parentCollection时,Item不会保存更新的属性:

 > 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。


所以我在 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: 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>]

我认为这说明已经为自动保存设置了关联。

然后我们可以得到对应的关联实例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> 

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

 > 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">]

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

4

1 回答 1

16

The problem here is the line

 i = c.items.first

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.

于 2012-07-23T08:47:55.130 回答