Note that there are some explanatory texts on larger screens.

plurals
  1. PO:autosave ignored on has_many relation -- what am I missing?
    text
    copied!<p>I have a pair of classes:</p> <pre><code>class Collection &lt; ActiveRecord::Base has_many :items, autosave: true end class Item &lt; ActiveRecord::Base belongs_to :collection end </code></pre> <p>From the <a href="http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html">docs</a>:</p> <blockquote> <p>When :autosave is true all children is saved, no matter whether they are new records:</p> </blockquote> <p>But when I update an <code>Item</code> and save its parent <code>Collection</code>, the <code>Item</code>'s upated attributes don't get saved:</p> <pre><code> &gt; c = Collection.first =&gt; #&lt;Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10"&gt; &gt; i = c.items.first =&gt; #&lt;Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25"&gt; &gt; i.name = 'new name' =&gt; "new name" &gt; c.save =&gt; true &gt; Collection.first.items =&gt; [#&lt;Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25"&gt;] </code></pre> <p>So, what am I missing?</p> <p>I'm using Rails 3.2.5 and Ruby 1.9.2.</p> <hr> <p>So I've done some digging about in the source of ActiveRecord. We can get hold of <code>c</code>'s autosave assocations:</p> <pre><code> &gt; c.class.reflect_on_all_autosave_associations =&gt; [#&lt;ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 @macro=:has_many, @name=:items, @options={:autosave=&gt;true, :extend=&gt;[]}, @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&gt;] </code></pre> <p>I think this illustrates that the association has been set up for autosaving.</p> <p>We can then get the instance of the association corresponding to <code>c</code>:</p> <pre><code> &gt; a = c.send :association_instance_get, :items =&gt; #&lt;ActiveRecord::Associations::HasManyAssociation:0x007fece738e920 @target=[#&lt;Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25"&gt;], @reflection=#&lt;ActiveRecord::Reflection::AssociationReflection:0x007fece57b3bd8 @macro=:has_many, @name=:items, @options={:autosave=&gt;true, :extend=&gt;[]}, @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&gt;, @owner=#&lt;Collection id: 1, name: "collection", created_at: "2012-07-23 00:00:10", updated_at: "2012-07-23 00:00:10"&gt;, @updated=false, @loaded=true, @association_scope=[#&lt;Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25"&gt;], @proxy=[#&lt;Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25"&gt;], @stale_state=nil&gt; </code></pre> <p>We can then find the actual objects that are associated via this association:</p> <pre><code> &gt; a.target =&gt; [#&lt;Item id: 1, collection_id: 1, name: "item1", created_at: "2012-07-23 00:00:25", updated_at: "2012-07-23 00:00:25"&gt;] </code></pre> <p>The object found here does not have update that I'd made earlier.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload