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 6814837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:44:09+00:00 2026-05-26T20:44:09+00:00

(Here’s my requisite line about being new to ruby/rails and programming in general; first

  • 0

(Here’s my requisite line about being new to ruby/rails and programming in general; first SO question as well)

I have a rake task to seed my MySQL DB from a CSV-

...
    CSV.read(uri).each do |row|
      Incident.create(
        :building_address => row[9],
        :agency => row[3],
        :complaint_type => row[6],
        :descriptor => row[5],
        :created => row[1],
        :closed => row[2],
        )
end

:created and :closed are defined as date fields (and are also separate from/unrelated to the rails created_at and updated_at fields), but I’m assuming CSV files recognize only strings.

When I run this task, day and month get switched around. So 1/12/2010 12:00:00 AM in the CSV (which for the CSV and myself in the US means January 12) ends up being parsed as 2010-12-1, and any day past 12 returns null, so 1/20/2010 12:00:00 AM results in a null entry in my DB.

I added

date:
    formats:
        default: "%m/%d/%Y"

to my en.yml, but that seems to only affect the display of a date, and has no effect on the creation of objects through my rake task. After searching around SO and other resources I tried

:created => row[1].strftime,

and

:created => row[1].strptime,

but got undefined method strptime/strftime for "Created Date":String (Created Date being the name of the column in the CSV).

So I also tried

:created => row[1].to_date

to change the (presumably) “date” string in the CSV to a date, but get an error undefined method '<' for nil:NilClass

Changing the date field in the CSV seems like a nonstarter as there are 1 million+ records.

How can I edit this rake task to properly seed my db from this CSV? Or should I change :created from a date to string and manipulate it in the application logic?

  • 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-05-26T20:44:10+00:00Added an answer on May 26, 2026 at 8:44 pm

    You could try using DateTime.strptime to convert your strings to DateTime instances:

    :created => DateTime.strptime(row[1], '%m/%d/%Y %H:%M:%S %p'),
    :closed  => DateTime.strptime(row[2], '%m/%d/%Y %H:%M:%S %p')
    

    The strptime class method will let you specify exactly the format you’re using so that none of the software has to guess. I’d recommend that you stick to ISO 8601 date and time formats for internal use in the future, leave the ambiguous formats for human consumption.

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

Sidebar

Related Questions

Here is what I have: Dim cmsManager As New Telerik.Cms.CmsManager() Dim currentNode As Telerik.Cms.Web.CmsSiteMapNode
Here's a strange question for you guys, I have a nice sorted list that
Here is what I want to do. Use this HTML line and have the
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here we go again, the old argument still arises... Would we better have a
Here's some of my production code (I had to force line breaks): task =
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is a great article about GC may occur at unexpected point of code

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.