As you can see from the XML in Listing 13-1, you have just one table and rows
element, so it will return every row element in the XML document.
Finally, now that you are guaranteed to have a connection, a model, and a correctly
structured database, you can begin inserting data into the database, as follows:
rows.each do |row|
AdResult.new do |n|
row.attributes.each do |attribute_name, attribute_value|
n.send("#{attribute_name}=", attribute_value)
end
n.save
end
end
This code loops through all of the rows, creating a new AdResult object for each. You
then loop through all the various attributes of each row, and use the send method to call
the setter method for that attribute. The send method takes a string naming the method
to call as well as a list of parameters. In other words, the following two lines are identical:
some_object.send('some_method', an_argument)
some_object.some_method(an_argument)
The advantage of using the send method is that you can call it with a method name
that you build dynamically, as you do here.
Pages:
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386