Ruby on Rails Wednesday, November 26, 2014



On Tuesday, 25 November 2014 03:58:01 UTC-5, Maneesh M P wrote:

Hello,
  
  I am trying to fetch instances details from amazon and trying to store in local mysql db. I am able to fetch all the details from amazon, but while storing the data in mysql i am getting the error undefined method `stringify_keys'  at two places [which ever gets executed first]
  
  Below is my code, can anyone please help me to resolve this error?  [ I marked error in red]

class Ec < ActiveRecord::Base
   attr_accessible :instance_id, :name, :public_ip_address, :availability_zone, :state, :created_at, :flavor_id, :account
   def sync
         properties = Array.new
         instance = Ec.new
         connection_dev = Fog::Compute.new({
           :provider                 => 'AWS',
           :aws_access_key_id        => '#######',
           :aws_secret_access_key    => ######'
       })

       dev_instances_list = connection_dev.servers.all
       
       dev_instances_list.each do | inst|
        begin
          instance[:instance_id] =  inst.id
          instance[:name] =   (inst.tags['Name'].to_s.empty?)? "NA" : inst.tags['Name']
          instance[:public_ip_address] =  inst.public_ip_address
          instance[:availability_zone] =  inst.availability_zone
          instance[:state] =  inst.state
          instance[:created_at] =  inst.created_at
          instance[:flavor_id] =  inst.flavor_id
          instance[:account] =  "abc"
          #instance.save
          properties.push(instance)
        rescue
            puts "exception"
        end
       end
      update_instance_details(properties)
   end

   def update_instance_details(properties)
       updated_list = Array.new
       to_store = Hash.new
       up = Ec.new
       Ec.find_each do |existing|
          to_store = properties.select{|a| a[:instance_id] == existing.instance_id}

`properties` here is an Array. `select` is always going to return an Array, even if one or no elements match.

          if(to_store.nil?)

So this will never work - if the instance is entirely missing, `to_store` will be `[]`.
 
            #someone has deleted this instance from amazon
            puts "Removing the record"
            existing.delete
          else
          #update existing record from amazon if there any changes
            Ec.update(existing.id, to_store)                  //Errror line

And then `update` is expecting a hash, not an Array - giving the error you've noted.

You may want `find` instead of `select` when extracting an instance's details from `properties`. This will return the *element* instead of an Array.

Example:

a = [1,2,3,4]
a.select { |el| el == 3 } # => returns [3]
a.find { |el| el == 3 } # => returns 3

For both cases, I'd recommend investigating exactly what class the values in `dev_instances_list` are. 

--Matt Jones

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8dca5134-48c1-4846-8d3d-6935721a64a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment