I am getting the XML from an external source via HTTPService post.
I am using CobraVsMongoose (CVM) to do a xml - hash conversion. One of the fields in the hash is "Task_Number", which is the id of the AR object that I want to retrive.
Here is an example of the object that CVM returns.
{"tasks"=>{"task"=>{"Task_Number"=>{"$"=>"3"}, "Task_Name"=>{"$"=>"task_three"}, "Priority_Rank"=>{"$"=>"55"}}}}
@my_tasks["tasks"]["task"].each do |hashed_task|
@task = Task.find(hashed_task["Task_Number"]["$"].to_i)
@task.priority = hashed_task["Priority_Rank"]["$"].to_f
@task.save
end
which works perfectly when I have many tasks, however, when I am sending over only one task i get this error in the development.log
TypeError (can't convert String into Integer):
I am pretty sure that "3".to_i should give me the integer 3. also, since the code runs smoothly for multiple tasks in the same XML document, I am baffled.
@my_tasks["tasks"]["task"] is an array of tasks, whereas in the case of
the single task, it gets parsed into a hash. My hack to fix this now is
to pass the same task twice if the number of tasks is only one, but this
is obviously a ugly hack.
Can anyone suggest how to change the rails code so that the
@my_tasks["tasks"]["task"] object looks the same, whether it has 1 or
many elements?