Ruby on Rails Tuesday, November 25, 2014


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}
          if(to_store.nil?)
            #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
            updated_list.push(existing.id)
          end
       end
         properties.select{|a| !(updated_list.include? a[:instance_id])} .each do |ins|
        puts "inserting new instance #{ins[:instnace_id].to_s}"
        new_instance = Ec.new(ins)                        /Error line
        new_instance.save
      end
   end

  Thanks,
Maneesh

--
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/a612135e-3b3a-4c66-82c9-c8d59ece6011%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment