Designer
Designer

Reputation: 1111

Reformating mail.date to compare with Date field in db if it exists. Ruby on Rails, ActionMailbox (mail gem)

I have the code below to get date from <%= form.date_select :date %> and compare if any post exists with the same date.

    # Get the date from form and format it to compare with db
    # date(1i)"=>"2021", "date(2i)"=>"3", "date(3i)"=>"22"
    @selected_date = Date.new(params["post"]["date(1i)"].to_i,
                              params["post"]["date(2i)"].to_i,
                              params["post"]["date(3i)"].to_i)

    # Check if the post exists in the db
    @existing_post = Post.where(user_id: current_user.id, date: @selected_date).first

This works well. Now I need to do that same with the date from email.

@selected_date = mail.date.to_s

But this mail date is in different format and comparing to date in db always fails. I can't use the ["date(1i)"] with mail.date.

How can I format this email.date so that I can compare the existence of it to date field in db?

Thank you


Update: Date format that comes from email as below I guess. Looking at the Logs on server

Date: Wed, 24 Mar 2021 09:57:57 +0000 enter image description here

Date format I have in the DB is as below. Output of Post.last in rails c

date: "2021-03-24 09:57:57.000000000 +0000 enter image description here

Upvotes: 0

Views: 83

Answers (1)

honey
honey

Reputation: 1077

  mail.date.to_time == date.to_time

If above statement isn't work then try

  mail.date.to_s.to_time == date.to_s.to_time

If you execute the following statement you will get the correct answer, try it in your console

  "Wed, 24 Mar 2021 09:57:57 +0000".to_time == "2021-03-24 09:57:57.000000000 +0000".to_time

Upvotes: 0

Related Questions