# A basic collections/hash question

**URL:** <https://www.ruby-forum.com/t/a-basic-collections-hash-question/175181>\
**Category:** Rails\
**Created:** [September 9, 2009, 11:23am UTC](https://www.ruby-forum.com/t/a-basic-collections-hash-question/175181 "2009-09-09T11:23:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [September 9, 2009, 11:23am UTC](https://www.ruby-forum.com/t/a-basic-collections-hash-question/175181/1 "2009-09-09T11:23:11Z")

</div>

Hi,

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 127.0.0.1 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?

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [September 9, 2009, 2:42pm UTC](https://www.ruby-forum.com/t/a-basic-collections-hash-question/175181/2 "2009-09-09T14:42:29Z")

</div>

Hi Mukund, thanks for the suggestions!

I did try

> params[:subscribers].each do |subscriber\_hash|  
> puts subscriber\_hash  
> my\_record = Subscriber.new(subscriber\_hash)  
> my\_record.save  
> end if params[:subscribers]

The result is that subscriber\_hash results in

subscriber  
nameSixmsidsm555imsi555imei555  
nameSevenmsidsm444imsi444imei444

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

Processing SubscribersController#import (for 127.0.0.1 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:in`new’  
app/controllers/subscribers\_controller.rb:49:in `import' app/controllers/subscribers_controller.rb:47:in`each’  
app/controllers/subscribers\_controller.rb:47:in `import' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/lib/ruby/1.8/webrick/server.rb:173:in`start\_thread’  
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start' /usr/lib/ruby/1.8/webrick/server.rb:162:in`start\_thread’  
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/lib/ruby/1.8/webrick/server.rb:92:in`each’  
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/lib/ruby/1.8/webrick/server.rb:23:in`start’  
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start’

Cheers.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [September 9, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/a-basic-collections-hash-question/175181/3 "2009-09-09T22:56:52Z")

</div>

Hi,

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)  
end

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:  
[http://railsforum.com/viewtopic.php?id=10888](http://railsforum.com/viewtopic.php?id=10888)

Regards.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [September 9, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/a-basic-collections-hash-question/175181/4 "2009-09-09T13:26:41Z")

</div>

params[:subscribers].each do |subscriber\_hash|  
my\_record = SubscriberModel.new(subscriber\_hash)  
my\_record.save  
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”]  
end  
end

* * *

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

On Sep 9, 2:23 pm, comopasta Gr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
