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
Marnen Laibow-Koser
2009-Oct-13 14:06 UTC
Re: Using transaction in non-active record model
Andrew Pace 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, > > AndrewBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.