In my Rails application I have these models
Order has_many OrderItem
OrderItem belongs_to Order
My controller has this (pseudo) code:
#before_filter
@cart = Order.find(session[:cart_id])
#Action: update_item
item = OrderItem.find(params[:item_id]
#The order, item belongs to, is in fact @cart, i.e. @cart.id == item.order.id
item.price = 999.99
item.order.total += item.price
After the last line the @cart
object still has the old total
value.
What's the best way to solve such situations in Rails? I guess the simplest way would be to just reload @cart
but perhaps there's a more Rails-like way.