New fields added after scaffold do not persist in the bank even though they are in Strong params

Asked

Viewed 69 times

1

I have a little problem here that I haven’t been able to solve for 3 days. Very strange. It’s in the editing of a record. He’s just updating the first record, not the second. The curious thing is that this only happens with the field quatidade_dev (added after scaffold) because the amount works normal. Including including in the temporary table that is where the problem is. I already put the new fields in Strong params but did not help.

By the exit of the console he seems to be getting it right:

See the fields quantity_dev below. Both with value 1.

Processing by OrdersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"MgDZLwYzZIp7BFd0kefREQIwpcL9g/LBKngJNdcFMNs=", "order"=>{"customer_id"=>"2", "tipo"=>"Consignado", "descontado"=>"35.0", "valor_total"=>"20.0", "item_total"=>"2", "order_num"=>"2016-37", "details_attributes"=>{"0"=>{"id"=>"356", "barcode"=>"CA001M300001000", "order_id"=>"37", "preco"=>"10.0", "cod_produto"=>"CA001", "desc_produto"=>"Calçinha teste", "cod_cor"=>"3", "desc_cor"=>"Rosa", "desc_tamanho"=>"M", "quantidade"=>"1", "quantidade_dev"=>"1", "total_dev"=>"10", "_destroy"=>"false"}, "1"=>{"id"=>"357", "barcode"=>"CA001P400001000", "order_id"=>"37", "preco"=>"10.0", "cod_produto"=>"CA001", "desc_produto"=>"Calçinha teste", "cod_cor"=>"4", "desc_cor"=>"Azul", "desc_tamanho"=>"P", "quantidade"=>"1", "quantidade_dev"=>"1", "total_dev"=>"10", "_destroy"=>"false"}}}, "commit"=>"Salvar", "id"=>"37"}

But when I try to insert them into a temporary table, one hour it passes the 14 parameters (1st record) and then just passes 12 (2nd record).

INSERT INTO "tmps" ("barcode", "cod_cor", "cod_produto", "created_at", "desc_cor", "desc_produto", "desc_tamanho", "order_id", "preco", "quantidade", "quantidade_dev", "total", "total_dev", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING "id" [["barcode", "CA001M300001000"], ["cod_cor", "3"], ["cod_produto", "CA001"], ["created_at", "2016-09-21 15:00:26.000000"], ["desc_cor", "Rosa"], ["desc_produto", "Calçinha teste"], ["desc_tamanho", "M"], ["order_id", "37"], ["preco", "10.0"], ["quantidade", 1], ["quantidade_dev", 1], ["total", 10.0], ["total_dev", "10.0"], ["updated_at", "2016-09-21 15:00:26.000000"]]

INSERT INTO "tmps" ("barcode", "cod_cor", "cod_produto", "created_at", "desc_cor", "desc_produto", "desc_tamanho", "order_id", "preco", "quantidade", "total", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["barcode", "CA001P400001000"], ["cod_cor", "4"], ["cod_produto", "CA001"], ["created_at", "2016-09-21 15:00:26.000000"], ["desc_cor", "Azul"], ["desc_produto", "Calçinha teste"], ["desc_tamanho", "P"], ["order_id", "37"], ["preco", "10.0"], ["quantidade", 1], ["total", 10.0], ["updated_at", "2016-09-21 15:00:26.000000"]]

If right after save I place a debug point, the values are not really saved in the Tails table (only the first record is saved, the second is not) If I go back and change the 2nd record and have it saved, then it saves both. But being empty he does not save others, only the first.

Could someone help ?

I put the controllers and Forms in this gist:

https://gist.github.com/rvgarimrj/55dd8949f976fb51b5208374266f6313

Follow the models:

Order.Rb

class Order < ActiveRecord::Base
    has_many :details, dependent: :destroy
    belongs_to :customer
    accepts_nested_attributes_for :details, reject_if: proc { |attributes| attributes['cod_produto'].blank? }, :allow_destroy => true
    validates :customer_id,
            :presence => true
    after_validation :sum_details
  # after_save :update_quantity
    after_create :set_order_num
  before_validation :set_percentage
  # after_commit :agrupa_detalhes
  # after_save :update_quantity

    TIPO = ['Venda',
    'Consignado',
    ]

    validates :tipo,
              inclusion: { in: TIPO }
    validates :tipo, presence:true
    validates :descontado,
            :presence => true,
            :numericality => true
    def valor_recebido
        if valor_total_dev.present?
            valor = ((valor_total - valor_total_dev) - (((valor_total - valor_total_dev) * descontado) / 100))
        else
            valor_total - ((valor_total * descontado)/100)
        end
    end
    def qtd_final
        if item_total_dev.present?
            qtd = item_total - item_total_dev
        else
            item_total
        end
    end
   private
   def update_quantity
    self.details.each do |d|
      # Atualiza estoque
      search = d.barcode
      variation = Variation.find_by(barcode:search)
      if d.quantidade_dev.present?
       qtd_detail = variation.quantity + d.quantidade_dev
       variation.quantity = qtd_detail
      elsif d.quantidade.present?
       qtd_detail = variation.quantity - d.quantidade
       variation.quantity = variation.quantity - d.quantidade
      end
      variation.save!
    end
   end

   def set_percentage
    if self.tipo == "Consignado"
      self.descontado = 35
    else
      self.descontado = 50
    end
   end
   def set_order_num
    update(order_num: "#{Date.current.year}-#{self.id}")

   end
   def sum_details
     total = 0
     qtd = 0
     total_dev = 0
     qtd_dev = 0
     self.details.each do |d|
      # Acumula valores
      total += (d.quantidade * d.preco) if d.quantidade.present?
      qtd += d.quantidade if d.quantidade.present?
      total_dev += (d.quantidade_dev * d.preco) if d.quantidade_dev.present?
      qtd_dev += d.quantidade_dev if d.quantidade_dev.present?

     end
     self.valor_total = total
     self.item_total = qtd
     self.valor_total_dev = total_dev if total_dev != 0
     self.item_total_dev = qtd_dev if qtd_dev != 0

   end
end

Detail.Rb

class Detail < ActiveRecord::Base

    belongs_to :order
  before_save :calcula_totais
  after_save :insere,:update_quantity
    def valor_recebido
      if total_dev.present?
        valor = ((total - total_dev) - (((total - total_dev) * self.order.descontado) / 100))
      else
        total - ((total * self.order.descontado)/100)
      end
    end


    private
  def insere
      o = self.order
      # debugger
        Tmp.destroy_all
        @detail = o.details.select("order_id,cod_produto,desc_produto,cod_cor,desc_tamanho,desc_cor,barcode, preco ,sum(quantidade) as quantidade, sum (total) as total,sum(quantidade_dev) as quantidade_dev, sum(total_dev) as total_dev").group("order_id,cod_produto,desc_produto,cod_cor,desc_cor,desc_tamanho,barcode,preco").order("cod_produto asc, desc_tamanho asc, desc_cor asc").flatten
        ActiveRecord::Base.transaction do
          @detail.each do |detail|
            time_created_at = Time.now.to_s(:db)
            time_updated_at = Time.now.to_s(:db)
            Tmp.create(order_id: detail.order_id,cod_produto: detail.cod_produto,desc_produto: detail.desc_produto,cod_cor: detail.cod_cor,desc_tamanho: detail.desc_tamanho,desc_cor: detail.desc_cor,preco: detail.preco,quantidade: detail.quantidade,created_at: time_created_at,updated_at: time_updated_at,total: detail.total,quantidade_dev: detail.quantidade_dev,total_dev: detail.total_dev,barcode: detail.barcode)
          end
        end
        o.details.destroy_all
        detail_items = Tmp.pluck(:order_id,:cod_produto,:desc_produto,:cod_cor,:desc_cor,:desc_tamanho,:preco,:quantidade,:total,:quantidade_dev,:total_dev,:barcode)
        Detail.import([:order_id,:cod_produto,:desc_produto,:cod_cor,:desc_cor,:desc_tamanho,:preco,:quantidade,:total,:quantidade_dev,:total_dev,:barcode], detail_items)
    end
    def get_quantity
        @qtd = self.quantidade
    end
  def calcula_totais
     if quantidade_dev.present?
      total_dev = quantidade_dev * preco
     else
      quantidade_dev = 0
     end
     if quantidade.present?
      total = quantidade * preco
     end
  end
    def update_quantity
        search = barcode
        variation = Variation.find_by(barcode:search)
         if quantidade_dev.present?
            qtd = variation.quantity + quantidade_dev
            variation.quantity = qtd
         elsif quantidade.present?
            qtd = variation.quantity - quantidade
            variation.quantity = variation.quantity - quantidade
         end
        variation.save!
    end
end
  • is quantidade_ror_dev or quantidade_dev ?

  • Campi is quantity_dev. The other is a class for jquery

  • I’ve reached the extreme. I dropped the quantity_dev column and created it again. Same problem. I went to the console and did the same operation and it worked. Because by the form you’re not saving?!?

  • Put the models and the schema.Rb too so you can see

  • What’s the mistake? Also put if you can

  • I put the models in question. No error, it simply passes the 14 parameters correctly of the 1st record and the others it only passes 12, ignoring quantity_dev and valor_dev...

  • Still no solution. Some soul could help ?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.