daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # I have an order
  2. class Order < ActiveRecord::Base
  3.   has_many :order_items
  4. end
  5. # with items
  6. class OrderItem < ActiveRecord::Base
  7.   belongs_to :family
  8.   belongs_to :order
  9.   has_many :order_items
  10.  
  11. # I sort the order items via a scope which joins with item family to sort them accordingly
  12.   scope :default_order, joins(:family).order('families.name, order_items.quantity')
  13. end
  14.  
  15. # Controller
  16. def edit
  17.    @order = Order.find params[:id]
  18.    @ois = @order.order_items.default_order  
  19. end
  20.  
  21. # My problem is in the update method, if we have errors and must show the order to the user...
  22. #    How do I sort my items?
  23. def update
  24.     ...
  25.     if @order.update_attributes(params[:order])
  26.        ...
  27.     else #it doesn't validate
  28.        #if I do
  29.        @ois = @order.order_items.default_order
  30.       # items are reloaded (with the correct order) BUT user input and validation errors are lost.
  31.       # how do I keep the sorting + data?!?
  32.     end
  33. end
RAW Paste Data
Top