text_field of rails 4 separated by comma and store it in the database

0

I have the following models in rails

payment_supplier.rb
class PaymentSupplier < ActiveRecord::Base
  has_many :folio_has_payment_suppliers
  has_many :folios, through: :folio_has_payment_suppliers, dependent: :destroy
end

folio_has_payment_supplier.rb
class FolioHasPaymentSupplier < ActiveRecord::Base
  belongs_to :folio
  belongs_to :payment_supplier
end

folio.rb
class Folio < ActiveRecord::Base
  has_many :folio_has_payment_suppliers
  has_many :payment_suppliers, through: :folio_has_payment_suppliers, dependent: :destroy
end

If I test from the console, the information is stored correctly

rails c --sandbox

ps = PaymentSupplier.new
ps.folio_ids
=> []
ps.folio_ids = [1, 4]
=> [1, 4]
ps.hotel_id = 87
ps.cantidad = 2500
ps.save
ps.folios (Me muestra los 2 folios almacenados, es decir me hace la union bien)

My problem is when I pass this to my partial payment_supplier # _form.html.erb, I save the data and save only the first value of the input, if I put ex. 1,4,7,10 only stores the 1, I infer that because for the input the values are read as a string, and in the console I pass the values to it as an array [1, 4], so I make a slight change to my model

class PaymentSupplier < ActiveRecord::Base
  has_many :folio_has_payment_suppliers
  has_many :folios, through: :folio_has_payment_suppliers, dependent: :destroy

  serialize :folio_ids, Array

  def folio_ids=(ids)
    self.folio_ids = ids.split(',')
  end
end

According to what I have read, what I am looking for is folio_ids to separate it by commas and store it, but when I add this last modification to my model, and I do the submit in the project the application hangs, checking in the console I get this

  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/models/payment_supplier.rb:11:in 'folio_ids='
  app/controllers/egr/payment_suppliers_controller.rb:16:in 'create'
    
asked by Uziel Trujillo 06.08.2016 в 18:45
source

2 answers

0

The solution I found was the following, my model payment_supplier.rb

class PaymentSupplier < ActiveRecord::Base
  # Associations
  belongs_to :hotel

  # Assosiations many to many
  has_many :folio_has_payment_suppliers
  has_many :folios, through: :folio_has_payment_suppliers, dependent: :destroy

  serialize :folio_ids, Array
  attr_accessor :folio_ids_text

  def folio_ids_text=(ids)
    self.folio_ids = ids.split(",").map(&:strip) # added map(&:strip)
  end
end

And in my partial I have payment_supplier # _form.html.erb

<%= f.text_field :folio_ids_text, :class => 'form-control' %>

My controller is as follows

def create
  @pagoProvedor = PaymentSupplier.new(ps_params)

  respond_to do |format|
    if @pagoProvedor.save
      format.html { redirect_to [ "egr", @pagoProvedor ], notice: 'El gasto ha sido creado' }
      format.json { render :show, status: :created, location: @pagoProvedor}
    else
      format.html { render :new }
      format.json { render json: @pagoProvedor.errors, status: :unprocessable_entity }
    end
  end
end

private
  def ps_params
    params.require(:payment_supplier).permit(..., :folio_ids_text)
  end
    
answered by 07.08.2016 / 19:16
source
0

I think you should not redefine folio_ids= , since that generates rails to handle the relationships with the other models. In fact, in your redefinition you entered an infinite loop that is what generates the error:

def folio_ids=(ids)
  self.folio_ids = ids.split(',')
end

Another thing, what is the intention of serialize :folio_ids, Array ? if it was to transform from string to array, it is not adequate. The goal of serialize is to convert any object to String and then save it in the bd, which I do not think this is the case (imagine you do not have a field folio_ids in the table payment_suppliers ). Another thing, why do you get a string instead of a fix? It is assumed that rails should serialize the parameters that arrive through the request (if you share the controller code, it would help). In case it is inevitable that you enter a string for folio_ids, you could set it up through a attr_accessor , something like:

    attr_accessor :folio_string

    def folio_string=(ids)
      self.folio_ids = ids.split(',') if ids.is_a? String
    end
    
answered by 07.08.2016 в 04:21