Using transaction in non-active record model

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

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

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