# Using transaction in non-active record model

**URL:** <https://www.ruby-forum.com/t/using-transaction-in-non-active-record-model/177193>\
**Category:** Rails\
**Created:** [October 13, 2009, 3:17am UTC](https://www.ruby-forum.com/t/using-transaction-in-non-active-record-model/177193 "2009-10-13T03:17:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![diabolist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@diabolist](https://www.ruby-forum.com/u/diabolist)\
**Post date:** [October 13, 2009, 3:17am UTC](https://www.ruby-forum.com/t/using-transaction-in-non-active-record-model/177193/1 "2009-10-13T03:17:41Z")

</div>

I want to use logical models to aggregate data to present information  
to the user. For example, I have a controller:

class OrdersController \< ApplicationController  
def

end

---

<div class="post-metadata">

**Author:** ![diabolist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@diabolist](https://www.ruby-forum.com/u/diabolist)\
**Post date:** [October 13, 2009, 3:37am UTC](https://www.ruby-forum.com/t/using-transaction-in-non-active-record-model/177193/2 "2009-10-13T03:37:36Z")

</div>

Sorry about above. The post should be:

I want to use logical models to aggregate data to present information  
to the user. For example, I might have a controller that looks like  
the following:

class OrdersController \< ApplicationController  
def new  
@order = Order.new  
@address = Address.new  
end  
end

What I would prefer to have is:

class OrdersController \< ApplicationController  
def new  
@logical\_order = LogicalOrder.new  
end  
end

Logical Order would look like (NOT and ActiveRecord model):

class LogicalOrder  
def order  
@order ||= Order.new  
end

def address  
@address ||= Address.new  
end

def save  
address\_saved = address.save  
order\_saved = order.save  
address\_saved && order\_saved  
end  
end

I have simplified this. There is an initialize and method\_missing  
method in order to hook it all up correctly.

My question is this. In this save method, it is not wrapped in a  
transaction block, like I would like. Since this model is not an  
active record model, how can I implement the save method such that it  
is wrapped in a transaction. I have thought about pushing the save  
logic back into one of the active record models (like the Order  
model), but I would prefer to do it within this class. Any thoughts?

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![diabolist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@diabolist](https://www.ruby-forum.com/u/diabolist)\
**Post date:** [October 13, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/using-transaction-in-non-active-record-model/177193/3 "2009-10-13T16:06:17Z")

</div>

Andrew P. wrote:

> Sorry about above. The post should be:
> 
> I want to use logical models to aggregate data to present information  
> to the user. For example, I might have a controller that looks like  
> the following:
> 
> class OrdersController \< ApplicationController  
> def new  
> @order = Order.new  
> @address = Address.new  
> end  
> end
> 
> What I would prefer to have is:
> 
> class OrdersController \< ApplicationController  
> def new  
> @logical\_order = LogicalOrder.new  
> end  
> end
> 
> Logical Order would look like (NOT and ActiveRecord model):
> 
> class LogicalOrder  
> def order  
> @order ||= Order.new  
> end
> 
> def address  
> @address ||= Address.new  
> end
> 
> def save  
> address\_saved = address.save  
> order\_saved = order.save  
> address\_saved && order\_saved  
> end  
> end

[…]

> Since this model is not an  
> active record model, how can I implement the save method such that it  
> is wrapped in a transaction. I have thought about pushing the save  
> logic back into one of the active record models (like the Order  
> model), but I would prefer to do it within this class. Any thoughts?

Just call the transaction from within your LogicalOrder code. What’s the  
problem?

> Thanks,
> 
> Andrew

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
