Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a date into rails using the console

I have a table that has a date field in it. How would I save the date using the console? I tried event = Event.create(name:"Concert", date:08/20/2016, location:'Portland', state:'OR') However, I am getting an Invalid octal digit error.

like image 519
Aaron Avatar asked Aug 16 '16 15:08

Aaron


People also ask

How do you create a date object in Ruby?

A Date object is created with Date::new , Date::jd , Date::ordinal , Date::commercial , Date::parse , Date::strptime , Date::today , Time#to_date , etc. require 'date' Date. new(2001,2,3) #=> #<Date: 2001-02-03 ...> Date.

How do I change the date format in Ruby on Rails?

For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.

What is Rails console?

Rails console is an irb session that is built into the rails environment and is accessed by typing rails c into the terminal. It can be used to test association methods, validations and check error messages when building a rails app.

How to quit Rails console?

To exit the console, type: quit .


2 Answers

You'll want to pass in an actual Date object, which you can get from a string with the Date.parse method:

event = Event.create(name: "Concert", date: Date.parse('2016-08-20'), location: 'Portland', state: 'OR')

Note that I've rewritten your date to be in a different format. The MM/DD/YYYY format is not portable across locales, so I'd strongly suggest you use YYYY-MM-DD (the ISO 8601 format).

like image 152
Robert Nubel Avatar answered Sep 20 '22 23:09

Robert Nubel


Using a string in the correct format will do the trick. For example:

>> foo = Foo.create date: "20/8/2016"
   (0.0ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "foos" ("date") VALUES (?)  [["date", Sat, 20 Aug 2016]]
   (0.9ms)  commit transaction
#<Foo id: 1, date: "2016-08-20">
>> foo.date
Sat, 20 Aug 2016

ActiveSupport provides core extensions on the String class for conversions from strings to date, time, and datetime. This is probably ok, and more convenient, to take advantage of while testing around in Rails console.

Taking advantage of this extension in the application itself (instead of explicitly parsing with Date.parse) is totally up to you and your team.

From the source:

"1-1-2012".to_date   # => Sun, 01 Jan 2012
"01/01/2012".to_date # => Sun, 01 Jan 2012
"2012-12-13".to_date # => Thu, 13 Dec 2012
"12/13/2012".to_date # => ArgumentError: invalid date

Just to be thorough, examples for String#to_time

"13-12-2012".to_time               # => 2012-12-13 00:00:00 +0100
"06:12".to_time                    # => 2012-12-13 06:12:00 +0100
"2012-12-13 06:12".to_time         # => 2012-12-13 06:12:00 +0100
"2012-12-13T06:12".to_time         # => 2012-12-13 06:12:00 +0100
"2012-12-13T06:12".to_time(:utc)   # => 2012-12-13 06:12:00 UTC
"12/13/2012".to_time               # => ArgumentError: argument out of range

And String#to_datetime:

"1-1-2012".to_datetime            # => Sun, 01 Jan 2012 00:00:00 +0000
"01/01/2012 23:59:59".to_datetime # => Sun, 01 Jan 2012 23:59:59 +0000
"2012-12-13 12:50".to_datetime    # => Thu, 13 Dec 2012 12:50:00 +0000
"12/13/2012".to_datetime          # => ArgumentError: invalid date
like image 29
James Avatar answered Sep 17 '22 23:09

James