0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Model#xxx_before_type_cast / Get instance / Not type casted inputs ( attributes ) / e.g invalid Date to be nil / #Rails

Last updated at Posted at 2020-03-05

# == Schema Information
#
# Table name: users
#
#  id                       :bigint           not null, primary key
#  birth_date               :date             not null

class User < ApplicationRecord
end

# Valid Date
# type casted 
# be Date
User.new(birth_date: "19800229").birth_date
# => Fri, 29 Feb 1980

# valid Date
# before Type Casted
# be String
User.new(birth_date: "19800229").birth_date_before_type_cast
# => "19800229"

# Invalid Date
# type casted 
# be nil
User.new(birth_date: "19800230").birth_date

# Invalid Date
# Get not type casted input 
User.new(birth_date: "19800230").birth_date_before_type_cast
# => "19800230"

Other methods

User.new.methods.grep /birth_date/
=> [:birth_date=,
 :birth_date,
 :birth_date?,
 :birth_date_changed?,
 :birth_date_change,
 :birth_date_will_change!,
 :birth_date_was,
 :birth_date_previously_changed?,
 :birth_date_previous_change,
 :restore_birth_date!,
 :saved_change_to_birth_date?,
 :birth_date_before_last_save,
 :saved_change_to_birth_date,
 :birth_date_change_to_be_saved,
 :will_save_change_to_birth_date?,
 :birth_date_in_database,
 :birth_date_before_type_cast]

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?