🖥

Model#xxx_before_type_cast / Get instance / Not type casted inputs ( a

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

https://github.com/YumaInaura/YumaInaura/issues/3009

チャットメンバー募集

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

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2020-03-05

Discussion