Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # I have an order
- class Order < ActiveRecord::Base
- has_many :order_items
- end
- # with items
- class OrderItem < ActiveRecord::Base
- belongs_to :family
- belongs_to :order
- has_many :order_items
- # I sort the order items via a scope which joins with item family to sort them accordingly
- scope :default_order, joins(:family).order('families.name, order_items.quantity')
- end
- # Controller
- def edit
- @order = Order.find params[:id]
- @ois = @order.order_items.default_order
- end
- # My problem is in the update method, if we have errors and must show the order to the user...
- # How do I sort my items?
- def update
- ...
- if @order.update_attributes(params[:order])
- ...
- else #it doesn't validate
- #if I do
- @ois = @order.order_items.default_order
- # items are reloaded (with the correct order) BUT user input and validation errors are lost.
- # how do I keep the sorting + data?!?
- end
- end
Add Comment
Please, Sign In to add comment