Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8287241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:58:53+00:00 2026-06-08T11:58:53+00:00

I have a pair of classes: class Collection < ActiveRecord::Base has_many :items, autosave: true

  • 0

I have a pair of classes:

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

class Item < ActiveRecord::Base
  belongs_to :collection
end

From the docs:

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

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

So, what am I missing?

I’m using Rails 3.2.5 and Ruby 1.9.2.


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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:58:54+00:00Added an answer on June 8, 2026 at 11:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a pair of base classes: class A {}; class B {
I have the following two classes: class Key<T1, T2> {} class Pair<F, S> {}
I have a simple pair of classes that looks like this: class X include
Working with the MVVM pattern, I have a pair of view model classes that
I've create some classes, so that I can have a dictionary with a pair
Is there a base class that I should inherit from that is just a
I am attempting to store objects derived from a templated base class in an
I'm having troubles trying to design classes. I have abstract Chip class, STM8 class
Playing around with type-classes I came up with the seemingly innocent class Pair p
I have a key-value pair like var classes = { red: 'Red', green: 'Green'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.