Rails 2 and RSS parsing gives weird errors

Finally! After many weeks I managed to track down a very strange bug. When parsing RSS feeds using Ruby’s built-in RSS::Parser, sometimes I would get bombs in Rails when trying to insert item.pubDate into the database.

Have you seen this one?

open location do |http|
  result = RSS::Parser.parse(http.read, false)
  result.items.each do |item|
    feed_items.create(
      :title => item.title,
      :link => item.link,
      :description => item.description,
      :published_at => item.pubDate
    )
  end
end

and then this error: ArgumentError in ‘Feed when fetching a feed should be valid’ wrong number of arguments (1 for 0)? I have and it has been driving me bonkers!

Turns out there is some nastiness in the item.pubDate and Rails’ built in methods to save time fields to a datetime column. The fix is easy, just tack a .to_s after:

open location do |http|
  result = RSS::Parser.parse(http.read, false)
  result.items.each do |item|
    feed_items.create(
      :title => item.title,
      :link => item.link,
      :description => item.description,
      :published_at => item.pubDate.to_s
    )
  end
end