Rails 6.1 allows attribute's default to be configured but keeping its type
source link: https://blog.saeloun.com/2020/11/15/rails-6-1-allows-attribute-default-to-be-configured-but-keeping-its-type
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Rails 6.1 allows attribute's default to be configured but keeping its type
Nov 15, 2020 , by Romil Mehta1 minute read
Rails allows us to change the type and set the default value of the attribute.
Let’s consider Message
class with sent_at
attribute of type datetime
.
class Message
end
> Message.type_for_attribute(:sent_at)
=> <ActiveRecord::Type::DateTime:0x00007fa01c1fd650 @precision=6, @scale=nil, @limit=nil>
> Message.new.sent_at
=> nil
We can change the sent_at
attribute’s type to integer
in the following way:
class Message
attribute :sent_at, :integer
end
> Message.type_for_attribute(:sent_at)
=> <ActiveModel::Type::Integer:0x00007fa01bd86ba8 @precision=nil, @scale=nil, @limit=nil, @range=-2147483648...2147483648>
We can also set the default value by providing a default
option:
class Message
attribute :sent_at, default: -> { Time.now.utc }
end
> Time.now.utc
=> 2020-10-15 12:10:16 UTC
> Message.new.sent_at
=> 2020-10-15 12:10:16 UTC
But the problem with this is, Rails also changes the type of the attribute when setting the default value, and drops the proper type:
> Message.type_for_attribute(:sent_at)
=> <ActiveModel::Type::Value:0x00007fd16d255418 @precision=nil, @scale=nil, @limit=nil>
Rails 6.1
Rails 6.1 has now fixed this issue.
class Message
attribute :sent_at, default: -> { Time.now.utc }
end
> Time.now.utc
=> 2020-10-15 12:10:16 UTC
> Message.new.sent_at
=> 2020-10-15 12:10:16 UTC
> Message.type_for_attribute(:sent_at)
=> <ActiveRecord::Type::DateTime:0x00007fa01c1fd650 @precision=6, @scale=nil, @limit=nil>
As we can see, the type here is set to ActiveRecord::Type::DateTime
based on the default value,
instead of dropping the type.
Share this post!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK