# ActiveRecord in Rails 2.1.0 misbehaving for Aggregate Attr

**URL:** <https://www.ruby-forum.com/t/activerecord-in-rails-2-1-0-misbehaving-for-aggregate-attr/142342>\
**Category:** Rails\
**Created:** [July 7, 2008, 3:41am UTC](https://www.ruby-forum.com/t/activerecord-in-rails-2-1-0-misbehaving-for-aggregate-attr/142342 "2008-07-07T03:41:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [July 7, 2008, 3:41am UTC](https://www.ruby-forum.com/t/activerecord-in-rails-2-1-0-misbehaving-for-aggregate-attr/142342/1 "2008-07-07T03:41:23Z")

</div>

I am not sure if this is a bug in ActiveRecord or is this the intended  
behavior. If it is then it does not make sense to me.

In a nutshell: If you change an aggregate object attribute created via  
composed\_of method (see Agile Web D. - Second Edition Chapter  
17 page 313 onwards) and then save the model, the change to the model  
(composite attribute specifically) will not be saved UNLESS you  
explicitly set the partial\_updates attribute for that model to false.  
N  
Specific use-case:

I have an Account model that shadows the accounts table in database.  
This account object contains a composite object (amongst others) called  
BillingAddress defined within the Account model class as follows:

composed\_of :billing\_address,  
:mapping =\> [%w(address\_1 address\_1), %w(address\_2 address\_2),  
%w(address\_3 address\_3),  
%w(city city), %w(state state), %w(zip zip),  
%w(card\_country country)] do |params|  
BillingAddress.new params  
end

To test this theory, in the Rails Console, I “found” and existing  
Account object in the database using

acct = Account.find(id) – where id is a specific account id

Now I create a billing address object:

ba = BillingAddress.new(“123 Lane”, nil, nil, “City”, “State”, “12345”,  
“USA”)

Next, I assigned the newly created billing object to the account  
object’s billing\_address composite attribute as follows:

acct.billing\_address = ba

Now if I save this as:

acct.save

Then no update statement is triggered as it does in Rails 2.0.2.

If however, I set the partial\_updates for the Account class to false  
BEFORE assigning the ba value to acct.billing\_address then the update  
statement is triggered. That is:

Account.partial\_updates = false  
acct.billing\_address = ba  
acct.save

This will trigger the update which is what I want.

Note that partial\_updates works as stated for simple attribute updates.  
That is:

Account.partial\_updates = true  
acct.address\_1 = ‘xyz street’  
acct.save

correctly triggers the partial update where only the address\_1 attribute  
is updated in the database.

Has anyone else experienced a similar problem with composite attributes?

Bharat

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [July 15, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/activerecord-in-rails-2-1-0-misbehaving-for-aggregate-attr/142342/2 "2008-07-15T17:39:17Z")

</div>

I confirm this behaviour on Rails 2.1.0. Seems like it’s a bug in  
rails.

On Jul 7, 2:41 am, Bharat R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
