# Transactions with two models

**URL:** <https://www.ruby-forum.com/t/transactions-with-two-models/109605>\
**Category:** Rails\
**Created:** [July 17, 2007, 8:16am UTC](https://www.ruby-forum.com/t/transactions-with-two-models/109605 "2007-07-17T08:16:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 17, 2007, 8:16am UTC](https://www.ruby-forum.com/t/transactions-with-two-models/109605/1 "2007-07-17T08:16:00Z")

</div>

Hi,

In Agile Web D. with Rails section on Transactions (p 381)  
there are two main examples.

1. making changes in two records in the same database table
2. making changes in two records each in a different _database_

I want to do what is in between: changes in two records each in a  
different table of the same database. Suppose they are apples and  
oranges tables.

How do I write this?

Do I need to be as elaborate as this and will it work?

apple = Apple.find(:first)  
orange = Orange.find(:first)

Apple.transaction(apple) do  
Orange.transaction(orange) do  
apple.update\_attributes(params[:apple])  
orange.update\_attributes(params[:orange])  
end  
end

Or can I just write something much simpler like this?

Apple.transaction(apple, orange) do  
apple.update\_attributes(params[:apple])  
orange.update\_attributes(params[:orange])  
end

Will the above be a problem since orange is not an Apple?

In the rails docs there is something even simpler

transaction do  
apple.update\_attributes(params[:apple])  
orange.update\_attributes(params[:orange])  
end

but I think this version won’t restore the apple and orange instances  
back to their previous state if the transaction fails.

What I don’t understand is the class name that preceeds  
“.transaction”. Does that class name make a transaction for just that  
table or with the entire _database_ associated with that table?

Any help greatly appreciated.

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 17, 2007, 9:30am UTC](https://www.ruby-forum.com/t/transactions-with-two-models/109605/2 "2007-07-17T09:30:12Z")

</div>

Peter M. wrote:

> I want to do what is in between: changes in two records each in a  
> different table of the same database. Suppose they are apples and  
> oranges tables.

[…]

> Or can I just write something much simpler like this?
> 
> Apple.transaction(apple, orange) do  
> apple.update\_attributes(params[:apple])  
> orange.update\_attributes(params[:orange])  
> end

This should work. Though the usage of instances as parameters to the  
transaction method is deprecated - check the docs.

> Will the above be a problem since orange is not an Apple?

Yes, the transaction affects the database connection not the individual  
tables.

> In the rails docs there is something even simpler
> 
> transaction do  
> apple.update\_attributes(params[:apple])  
> orange.update\_attributes(params[:orange])  
> end
> 
> but I think this version won’t restore the apple and orange instances  
> back to their previous state if the transaction fails.

Because this behaviour is deprecated.

You can check your development log to see exactly what queries are  
executed, that should show you the transactions also.

–  
Cheers,

- Jacob A.
