: автосохранение игнорируется в отношении has_many - что мне не хватает?

У меня есть пара классов:

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

class Item < ActiveRecord::Base
  belongs_to :collection
end

Отдокументы:

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

Но когда я обновляюItem и сохранить его родителяCollection,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">]

Обнаруженный здесь объект не имеет обновления, которое я сделал ранее.

Ответы на вопрос(1)

Ваш ответ на вопрос