activerecord : undefined method stringify_keys error

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

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