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::Baseattr_accessible :instance_id, :name, :public_ip_address, :availability_zone, :state, :created_at, :flavor_id, :accountdef syncproperties = Array.newinstance = Ec.newconnection_dev = Fog::Compute.new({:provider => 'AWS',:aws_access_key_id => '#######',:aws_secret_access_key => ######'})dev_instances_list = connection_dev.servers.alldev_instances_list.each do | inst|begininstance[:instance_id] = inst.idinstance[:name] = (inst.tags['Name'].to_s.empty?)? "NA" : inst.tags['Name'] instance[:public_ip_address] = inst.public_ip_addressinstance[:availability_zone] = inst.availability_zoneinstance[:state] = inst.stateinstance[:created_at] = inst.created_atinstance[:flavor_id] = inst.flavor_idinstance[:account] = "abc"#instance.saveproperties.push(instance)rescueputs "exception"endendupdate_instance_details(properties) enddef update_instance_details(properties) updated_list = Array.newto_store = Hash.newup = Ec.newEc.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 amazonputs "Removing the record"existing.deleteelse#update existing record from amazon if there any changesEc.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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment