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 3336786
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:10:38+00:00 2026-05-18T00:10:38+00:00

Why can’t I save changes in an associated object saving a current object if

  • 0

Why can’t I save changes in an associated object saving a current object if attributes of the first one are represented by a value object?

For example, I have a simple e-commerce application. It use a Client model for manipulating clients

Client model


# db/migrate/<...>_create_clients.rb

class CreateClients < ActiveRecord::Migration
  def self.up
    create_table :clients do |t|
      t.string :name
    end
  end

  def self.down
    drop_table :clients
  end
end


# app/models/client.rb

Client < ActiveRecord::Base
  has_one :balance
end

and an associated Balance model for holding thier balances.

Balance model


# db/migrate/<...>_create_balances.rb

class CreateBalances < ActiveRecord::Migration
  def self.up
    create_table :balances do |t|
      t.integer    :amount
      t.string     :currency
      t.references :client
    end
  end

  def self.down
    drop_table :balances
  end
end


# app/models/balance.rb

class Balance < ActiveRecord::Base
  belongs_to :client

  composed_of :money,
    :mapping     =>
      [%w{amount cents}, %w{currency currency_as_string}],
    :constructor =>
      ->(amount, currency) { Money.new(amount || 0, currency || 'RUB') }
end

The Balance model uses a Money object from an external library called Money. The object represents model’s attributes amount and currency adding to the model useful methods for manipulating those attributes.

Gemfile


# Gemfile

gem 'money'

I have some seed data in seeds.rb.

Seeds


# db/seeds.rb

elena = Client.create(:name => 'Elena')
elena.build_balance.money = Money.new(0, 'RUB')
elena.save

When I try to change a balance of the client it isn't changed in spite of the save method of the current object returns true.


>> elena = Client.find_by_name('Elena')
=> #<Client id: 1, name: "Elena">

>> elena.balance
=> #<Balance id: 1, amount: 0, currency: "RUB", client_id: 1>

>> elena.balance.money
=> 0.00

>> elena.balance.money += Money.new(50000, 'RUB')
=> 500.00

>> elena.save
=> true

# log/development.log
# no changes

However, I can save the changes using the following two ways.

1.


>> elena = Client.find_by_name('Elena')
=> #<Client id: 1, name: "Elena">

>> balance = Balance.find(elena.id)
=> #<Balance id:1 , amount: 0, currency: "RUB", client_id: 1>

>> balance.money += Money.new(50000, 'RUB')
=> 500.00

>> balance.save
=> true

# log/development.log
# UPDATE "balances" SET "amount" = 50000 WHERE ("balances"."id" = 1)

2.


>> elena = Client.find_by_name('Elena')
=> #<Client id:1, name:"Elena">

>> elena.balance.money += Money.new(50000, 'RUB')
=> 500.00

>> elena.balance.save
=> true

# log/development.log
# UPDATE "balances" SET "amount" = 50000 WHERE ("balances"."id" = 1)

Despite I can save changes of a balance using the aforementioned ways I would like to know why I can't do it using the “traditional” <current_model>.save method.

Thanks.

Debian GNU/Linux 5.0.6;

Ruby 1.9.2;

Ruby on Rails 3.0.1;

Money 3.1.5.

  • 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-05-18T00:10:38+00:00Added an answer on May 18, 2026 at 12:10 am

    It is accomplished by doing this:

    # app/models/client.rb
    
    Client < ActiveRecord::Base
      has_one :balance, :autosave => true
    end
    

    The problem with the association before is that when you were invoking elena.save, it was only looking to see if the Client object had changed. If you performed a check like this on the first example:

    >> elena.changed?
    => false
    >> elena.balance.changed?
    => true
    

    If you don’t inform ActiveRecord to check the associated models it will be lazy and ignore changes when saving the parent model.

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

Sidebar

Related Questions

If all tables I want to delete from have the column gamer_id can i
Can i get the source code for a WAMP stack installer somewhere? Any help
I need to solve the following question which i can't get to work by
i have a input tag which is non editable, but some times i need
I have a new web app that is packaged as a WAR as part
We manage a site for a medical charity. They have a number of links
I want to use a temp directory that will be unique to this build.
Every time that I want to do a Layout, I'm getting a black layout
I'm trying to build a C++ extension for python using swig. I've followed the
I'm in the process of porting some code from Linux to Mac OS X.

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.