# Problem in updating the association object attributes

**URL:** <https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380>\
**Category:** Rails\
**Created:** [December 28, 2007, 6:05am UTC](https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380 "2007-12-28T06:05:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthikeyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@karthikeyan](https://www.ruby-forum.com/u/karthikeyan)\
**Post date:** [December 28, 2007, 6:05am UTC](https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380/1 "2007-12-28T06:05:22Z")

</div>

Hi All,

I am not able to update the association’s object attributes along with  
my model object.

For example, my models are,

class Library  
has\_many :books  
end

class Book  
belongs\_to :library  
end

I am getting the attribute values for both the models from the view and  
I try to update both in a single transaction. When I tried like,

@book.attributes = params[:book]  
@book.library.attributes = params[:library]  
@book.save

It is updating the books table but not the libraries table.  
But when I try the same logic for new book and new library creation, it  
works fine. The problem is only when updating.

Now i have implemented as,

@book.update\_attributes(params[:book])  
@book.update\_attributes(params[:library])

But I want to update both the attributes in a single statement and in a  
single transaction.

Any suggestions???

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![karthikeyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@karthikeyan](https://www.ruby-forum.com/u/karthikeyan)\
**Post date:** [December 28, 2007, 6:17am UTC](https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380/2 "2007-12-28T06:17:32Z")

</div>

afaik you can’t do anything like that.

Also it should be:

book.library.update\_attributes(params[:library])

On Dec 28, 2007 3:35 PM, Karthi kn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> @book.save  
> But I want to update both the attributes in a single statement and in a  
> single transaction.
> 
> Any suggestions???
> 
> ## Thanks in advance…
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![karthikeyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@karthikeyan](https://www.ruby-forum.com/u/karthikeyan)\
**Post date:** [December 28, 2007, 6:20am UTC](https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380/3 "2007-12-28T06:20:05Z")

</div>

Ryan B. wrote:

> afaik you can’t do anything like that.
> 
> Also it should be:
> 
> book.library.update\_attributes(params[:library])

Yes. Sorry. That was my typing mistake. But, is it not possible to  
update both in a single transaction?

---

<div class="post-metadata">

**Author:** ![karthikeyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@karthikeyan](https://www.ruby-forum.com/u/karthikeyan)\
**Post date:** [December 28, 2007, 6:23am UTC](https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380/4 "2007-12-28T06:23:23Z")

</div>

Like I said, no there isn’t as far as I know.

On Dec 28, 2007 3:50 PM, Karthi kn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Yes. Sorry. That was my typing mistake. But, is it not possible to update both in a single transaction?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![karthikeyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@karthikeyan](https://www.ruby-forum.com/u/karthikeyan)\
**Post date:** [December 28, 2007, 6:24am UTC](https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380/5 "2007-12-28T06:24:35Z")

</div>

Ok. Thanks.

---

<div class="post-metadata">

**Author:** ![karthikeyan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@karthikeyan](https://www.ruby-forum.com/u/karthikeyan)\
**Post date:** [December 28, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/problem-in-updating-the-association-object-attributes/124380/6 "2007-12-28T18:15:04Z")

</div>

On Dec 28, 2007 12:20 AM, Karthi kn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Yes. Sorry. That was my typing mistake. But, is it not possible to  
> update both in a single transaction?

Assuming that the tables are in the same database, you can do it in a  
single transaction, albeit not in a single save.

Library.transaction do  
@library.update\_attributes(params[:book])  
@book.update\_attributes(params[:library])  
end

Whether you use Library or Book as the receiver of the transaction  
method shouldn’t matter since the transaction applies to the database  
connection which is shared assuming, as I said, that both tables are  
in the same DB.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
