Repository navigation
Allow attribute's default to be configured but keeping its own type - #39830
Merged
Merged
Conversation
eugeneius
approved these changes
Jul 13, 2020
Member
There was a problem hiding this comment.
Suggested change
| attribute :written_at, defaullt: -> { Time.now.utc } | |
| attribute :written_at, default: -> { Time.now.utc } |
```ruby
class Post < ActiveRecord::Base
attribute :written_at, default: -> { Time.now.utc }
end
# Rails 6.0
Post.type_for_attribute(:written_at) # => #<Type::Value ... precision: nil, ...>
# Rails 6.1
Post.type_for_attribute(:written_at) # => #<Type::DateTime ... precision: 6, ...>
```
This is an alternative of rails#39797.
Context rails#39797 (comment).
If people intend to override the existing type on the attribute, usually
an overriding type is explicitly specified, so I agree that the current
behavior (drop existing type information if type is omitted) is
practically quite useless, and it is almost like a bug in that there is
no way to override just the default.
So I'd like to change the current behavior on existing attributes
without a deprecation as a fix.
Closes rails#39797.
See also kufu/activerecord-bitemporal#57.
kamipo
force-pushed
the
lazy_construct_attribute_type_2
branch
from
July 13, 2020 22:04
349c27f to
8c793aa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an alternative of #39797.
Context #39797 (comment).
If people intend to override the existing type on the attribute, usually
an overriding type is explicitly specified, so I agree that the current
behavior (drop existing type information if type is omitted) is
practically quite useless, and it is almost like a bug in that there is
no way to override just the default.
So I'd like to change the current behavior on existing attributes
without a deprecation as a fix.
Closes #39797.
See also kufu/activerecord-bitemporal#57.