Home > Back-end >  Ruby on Rails pushing data into array after looping doesn't save data
Ruby on Rails pushing data into array after looping doesn't save data

Time:07-19

Created a method to push data into an array, but after I only get the original data, I tried .save but it also doesn't work.

  def full_qualify_representative(client)
    unless client.customer_types.nil?
      full_rep = [client.capacity]   ["Representado por "]
      # simple array works ok   
      client.customer_types.each do |ct|
        retrieve_represented(ct.represented)
        full_rep << full_qualify(Client.find_by_id(ct.represented))
        # here it works, it comes the original   the new array given by the method full_qualify 
        # tried .save but it doesn't work
      end
      full_rep
      # it returns only the original one 
    end
  end

CodePudding user response:

I think that default full_rep is an array. After that, loop and add in array.

def full_qualify_representative(client)
    full_rep = []
    unless client.customer_types.nil?
      full_rep = [client.capacity]   ["Representado por "]     
      client.customer_types.each do |ct|
        retrieve_represented(ct.represented)
        full_rep << full_qualify(Client.find_by_id(ct.represented))
      end
    end
    full_rep
  end

CodePudding user response:

I tried to recreate my problem with a simple code using only ruby and some simple arrays and it worked well, no bugs. So I figured out it was something I was doing...

This is the code I used to test with simple ruby:

client = {
  :capacity => ["Capable", "Incapable"],
  :customer_types => ["Capable", "Incapable"],
  :outro => "yes" }


def full_qualify_representative(client)
  unless client[:customer_types].nil?
    full_rep = []
    client[:customer_types].each do |ct|
      full_rep << client[:customer_types]
    end
    full_rep
  end
end

puts full_qualify_representative(client)

So I went back to my code and did some manual debugging, and I figured out I was using one method inside another, creating some kind of infinite looping and my code didn't work. I don't know to explain why... but after I removed the looping it worked out.

code

result1

result2

  • Related