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.
It is accomplished by doing this:
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:
If you don’t inform ActiveRecord to check the associated models it will be lazy and ignore changes when saving the parent model.