Rails Console Displays a date for a Datetime column only.

I have two columns in my Reminders model ( date and mail_date ) that have a datetime column type, as you can see in my schema.rb :

 create_table "reminders", force: :cascade do |t| t.string "name" t.datetime "date" t.boolean "reminder", default: false t.string "repeating" t.boolean "approved" t.boolean "gift", default: false t.boolean "gift_help", default: false t.string "occasion_type", default: "Other" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "slug" t.datetime "mail_date" end 

However, in my console they only appear as dates:

 [33] pry(main)> Reminder.where(name: "Mentor Call") Reminder Load (0.4ms) SELECT "reminders".* FROM "reminders" WHERE "reminders"."name" = ? [["name", "Mentor Call"]] => [#<Reminder:0x007fe32b13f090 id: 8, name: "Mentor Call", date: Thu, 27 Oct 2016, reminder: true, repeating: "Weekly", approved: nil, gift: false, gift_help: false, occasion_type: "Other", user_id: 1, created_at: Thu, 13 Oct 2016 01:59:57 UTC +00:00, updated_at: Fri, 21 Oct 2016 23:47:16 UTC +00:00, slug: "mentor-call", mail_date: Thu, 27 Oct 2016>] 

I know that datetime data is saved because it is displayed on my view page, where is it ...

 October 27, 2016 07:00 Mentor Call ... 2016-10-27 07:00:00 UTC 

... is created from this erb:

 <% @reminders.each do |o| %> <tr> <td> <%= o.date.try(:to_formatted_s, :long) %> </td> <td> <%= link_to reminder_path(o), style: "color: black" do %> <strong><%= o.name %></strong> <% end %> ... <td> <% if o.reminder %> <%= o.mail_date.try(:to_formatted_s, "%m/%d/%Y") %> <% else %> None, you're on your own. <% end %> </td> </tr> 

I am trying to create a mailer that is sent to mail_date , but I am having problems (I think) due to this datetime vs date problem. Can anyone straighten me out?

+2
source share
1 answer

I suspect the date column name is from Rails or Ruby reserved words . Change the name of the date column to something else, like remind_at , and this should do the trick:

 create_table "reminders", force: :cascade do |t| t.string "name" t.datetime "remind_at" t.boolean "reminder", default: false # ... end 
+1
source

All Articles