Rails cannot correctly populate datetime type values.

Asked 2 years ago, Updated 2 years ago, 362 views

Prerequisites

Data is not correctly placed in the datetime type column of your own application (Railsc does).
I don't know why, so please help me.

What do you want to do

At the time of user creation, I would like to include data of datetime type like created_at.

Problems/Error Messages you are experiencing

[[["email", "xxxxx.com",
] ["encrypted_password", "[FILTERED]",
["created_at", "2022-10-16 21:32:18.541640", "updated_at", "2022-10-16 21:32:18.541640", "name", "xxxx", "birthday", "2022-09-25", "received_at", nil]

I would like this received_at to be included in the same way as created_at at the time of user creation.

Source Codes Affected


before_action —Authenticate_user!

  def new
    @user=User.new
  end

  def initialize
    @user.received_at=params [received_at::Time.current]
  end

  def create
    @user=User.new(user_params)
    @user.received_at =Time.now
    [email protected]
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url, notice: "Please check your email to activate your account."
    else
      render 'new'
    end
  end

Tried

Actually, I was re-creating this app itself, and it worked with the following code. (Received_at got the expected value.)

def initialize
    @user.received_at=params [received_at::Time.now]
  end


  def create
    @user=User.new(user_params)
    # Please note that the params[:user] implementation is not finished!
    # mass assignment vulnerability
    [email protected]
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url, notice: "Please check your email to activate your account."
    else
      render 'new'
    end
  end

So once you copy the code as it is, it doesn't work for some reason.

Also, when I checked from the console, it was done there, so I don't think it's because I didn't add any columns.

irb(main): 001:0>user=User.create
  TRANSACTION (0.1ms) BEGIN
=>#<User id:nil, email:", created_at:nil, updated_at:nil, name:nil, birthday:nil, received_at:nil>
irb(main): 002:0>user.received_at = Time.now
=>2022-10-16 21:04:36.530002067+0900
irb(main): 003:0>user
# <User id:nil,email:",created_at:nil,updated_at:nil,name:nil,birthday:nil,received_at:"2022-10-16 21:04:36.530002000+0900">
=>#<User id:nil, email:", created_at:nil, updated_at:nil, name:nil, birthday:nil, received_at:"2022-10-16 21:04:36.530002000+0900">

Supplementary information (for example, FW/Tool Version)

Ruby 3.1.2
Rails 7.0.4
Implement user registration with the Device Gem

ruby-on-rails ruby deviation

2022-10-21 00:01

1 Answers

Is received_at reflected in the DB in rails db:migrate after creating a migration to add columns to the users table such as rails generate migration AddReceivedAtToUser received_at:datetime?app/models/user.user:received_code> class

In the latter case, only virtual columns are added on rails and saved does not reflect in DB.DB The column itself does not exist in the DB users table.

"Regarding ""You can put it in the rails console,"" have you checked if it is reflected in the DB?" Specifically, if you reload from DB after saving like user.save;user.reload, is received_at not nil?

"Regarding ""re-creating the app itself,"" have you taken over the contents of the migration file?"

I think it would be good if you could confirm the above points first.

In fact, I don't think received_at is directly related to not being registered, so the rest of the content is interesting.
The initialize method in the code provided has a lot of thrusts.Does it actually exist?
Place of entry:


2022-10-21 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.