Problem in updating the association object attributes

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…

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 [email protected]
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/.


Ryan B.

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

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?

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

On Dec 28, 2007 3:50 PM, Karthi kn [email protected]
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/.


Ryan B.

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

Ok. Thanks.

On Dec 28, 2007 12:20 AM, Karthi kn [email protected]
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/