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.
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.
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
Regards.
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 [email protected]