I am working on a project where there is a Stock of Bidons (Drums) , but I would like to edit the main Stock of this when that Stock is taken in another model , for example when you make a purchase of a main product and this Stock is subtracted with the one you enter.
The models are:
Buy
class Compra < ActiveRecord::Base
belongs_to :bidon #De acá recogemos el Stock principal
end
Bidon
class Bidon < ActiveRecord::Base
has_many :compras
end
In the shopping model, I have access to : bidon_id , I can select the drum, but I am saving the quantity with another field called: quantity_buckets. The name of the field in the Bidons model is only : quantity and you thought that in the buyer_controller you could subtract the : quantity_buckets with the : quantity of the Bidons model to achieve the discount of the Main Stock when creating the purchase. This I will do with other modules, so I need some guidance please.
compras_controller.rb
def new
@compra = Compra.new
@bidons = Bidon.all.map{ |b| [b.id] }
end
def create
@compra = Compra.new(compra_params)
@compra.bidon_id = params[:bidon_id]
end
private
# Use callbacks to share common setup or constraints between actions.
def set_compra
@compra = Compra.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def compra_params
params.require(:compra).permit(:proveedor_id, :cantidad_bidones, :calibradas, :calibre, :tipo, :bidon_id)
end
end
In the purchase form I have the following:
<div class="field">
<%= f.label :cantidad_bidones %><br>
<%= f.number_field :cantidad_bidones %>
</div>
<div class="field">
<%= select_tag(:bidon_id, options_for_select(@bidons), :prompt => "Seleccione los Bidones") %>
</div>
Only that would be my query, how to modify the existing data in the Bidons model in this case : quantity