Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:30:57+00:00 2026-06-14T13:30:57+00:00

I have written a rake task to import an XML feed into my ActiveRecord

  • 0

I have written a rake task to import an XML feed into my ActiveRecord model but am running into some trouble – the XML feed will not publish any columns that are empty and this breaks my migration tool. How can I design my importer so it skips over the empty fields?

My importer looks something like this

desc "Import XML Feed into Items Database v20121116" 
task :new_import_items => :environment do

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::XML(File.open("#{Rails.root}/lib/tasks/datafeed.xml"))

actions = doc.xpath("/merchantProductFeed/merchant/prod") 

actions.each do |action|

a = Item.where("affiliate_product_id = ?", action.css("pId").text).first


if a != nil

  a.update_attributes(

    :brand => action.at('brandName').text, 
    :description => action.at('desc').text, 
    :regular_price => action.at('buynow').text,

    ....

Let’s say “desc” is not available on the xml feed. I’d like my code to ignore “desc” in that instance.

The error message is useless:

undefined method `text' for nil:NilClass

But it has nothing to do with the text method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:30:58+00:00Added an answer on June 14, 2026 at 1:30 pm

    You can use the try method:

    if a != nil
      a.update_attributes(
        :brand => action.at('brandName').try(:text), 
        :description => action.at('desc').try(:text), 
        :regular_price => action.at('buynow').try(:text),
    
        ....
    

    The doc says:

    If the receiving object is a nil object or NilClass: a NoMethodError exception will not be raised and nil will be returned instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have written some RoR sites in the past, but never bothered too much at
I have written rake task like as below, namespace :db do desc load photo
I have written some Python scripts in Eclipse where at some point I have
I have written a raw binary image file into a buffer which is an
I have some additional scripts that I've written outside of my Rails 3.1.x app,
i have written a code for insertion sort but it is nt sorting.. kindly
I have a roles model in a rails app that I have written a
I am trying to document some rake tasks I've written using RDoc comments in
I have written the code for getting the blank cell information but the problem
I have Written the code to store the bitmap Image in sdcard But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.