A basic collections/hash question


I give up trying. I need to ask this basic question about collections
and hashes.

I’m trying to do a batch update of records using XML:

<?xml version="1.0" encoding="UTF-8"?> 111 111 111 Six 777 777 777 Seven

I have this in the routes:

map.resources :subscribers, :collection => {:import => :post}

When I post the XML I get:

Processing SubscribersController#import (for at 2009-09-09
12:18:22) [POST]
Parameters: {“action”=>“import”,
“subscribers”=>{“subscriber”=>[{“name”=>“Six”, “msidsm”=>“111”,
“imsi”=>“111”, “imei”=>“111”}, {“name”=>“Seven”, “msidsm”=>“777”,
“imsi”=>“777”, “imei”=>“777”}]}, “controller”=>“subscribers”}

How can I easily create subscriber records for those I send in the XML?
I’ve managed to extract the data but it fails when only one subscriber
element is included in the XML.

What would be the best way to do this?


Hi Mukund, thanks for the suggestions!

I did try

params[:subscribers].each do |subscriber_hash|
puts subscriber_hash
my_record = Subscriber.new(subscriber_hash)
end if params[:subscribers]

The result is that subscriber_hash results in


Ok so far I guess. But then Subscriber.new(subscriber_hash) fails as
seen below:

Processing SubscribersController#import (for at 2009-09-09
15:36:05) [POST]
Parameters: {“action”=>“import”,
“subscribers”=>{“subscriber”=>[{“name”=>“Six”, “msidsm”=>“555”,
“imsi”=>“555”, “imei”=>“555”}, {“name”=>“Seven”, “msidsm”=>“444”,
“imsi”=>“444”, “imei”=>“444”}]}, “controller”=>“subscribers”}

NoMethodError (undefined method stringify_keys!' for #<Array:0xb6041008>): app/controllers/subscribers_controller.rb:49:innew’
app/controllers/subscribers_controller.rb:49:in import' app/controllers/subscribers_controller.rb:47:ineach’
app/controllers/subscribers_controller.rb:47:in import' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:162:in start' /usr/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/lib/ruby/1.8/webrick/server.rb:92:ineach’
/usr/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/lib/ruby/1.8/webrick/server.rb:23:instart’
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start’



I managed to get over this problem.

The code I’m using now is:

data = params[:subcribers][:subscriber]
(data.is_a?(Array) ? data : [data]).each do |d|
my_record = Report.create(d)

This works fine wether I have only one subscriber or many in the XML
Next will be doing a batch update. But that should be a different story
since my problem was doing the iteration with the data.

I found the hints from here:


params[:subscribers].each do |subscriber_hash|
my_record = SubscriberModel.new(subscriber_hash)
end if params[:subscribers]

Assuming that SubscriberModel is an activerecord model that maps to a
table with similar layout as the hash.

Otherwise, define your own class with an initializer method like so
and do whatever you want with it.

class SubscriberModel
attr_accessor :name, :msidsm, :imsi, :imei
def initialize(init_hash={})
@name = init_hash[“name”]
@msidsm = init_hash[“msidsm”]
@imsi = init_hash[“imsi”]
@imei = init_hash[“imei”]

Alternatively, I could be way off base. Let me know if it works for
you. :stuck_out_tongue:

On Sep 9, 2:23 pm, comopasta Gr [email protected]