In my app I have model PurchaseOrder and PurchaseOrderList. When I create a purchase order it updates both tables purchase_orders and purchase_order_lists with a nested form.
Now I want to be able to add a button update stock on purchase_orders/show. When I click this button a new record will be created on table stockdiaries with same products, units and prices as on the purchase order.
The logic is that first I create a purchase order and then I click the button to update stock when goods are received.
The best I came up with is to create this method on PurchaseOrders controller:
def update_stock
@purchase_order_list = PurchaseOrderList.where(PURCHASE_ORDER: params[:ID])
@stockdiary = Stockdiary.create(PRODUCT: @purchase_order_list.PRODUCT, UNITS: @purchase_order_list.UNITS, PRICEBUY: @purchase_order_list.PRICEBUY)
flash[:notice] = "Stock updated successfully."
redirect_to(:action => 'show', :ID => @purchase_order.ID)
end
and in my purchase_orders/show:
<%= link_to "Update Stock", { controller: :purchase_orders, action: :update_stock, ID: @purchase_order.ID} %>
but it raises error
undefined method `PRODUCT' for
#<PurchaseOrderList::ActiveRecord_Relation:0x007f1efeff4898>
But there is a column PRODUCT on purchase_order_lists table.
Note that I included in the method only columns that are common to purchase_order_lists and stockdiaries as others (as id or status) are not concerned on this question. Columns names are capital as I'm building the app on existing db.
What is the correct way to create a stockdiary from a purchase order?