Repository navigation
Conversation
I've been reported this issue from a friend.
We'd like to define attribute by mixin, espetially to specify/overload
default value.
e.g.
```ruby
module Mod
included do
attribute :reserved_column, :datetime, default: SENTINEL_VALUE
end
end
```
Unfortunately, it will lose the original datetime precision.
I suggested using `define_attribute` and `type_for_attribute`
(`define_attribute :attr, type_for_attribute(:attr), default: "..."`),
but it will force `load_schema!` at mixin time.
It means that there is no way that overload default but maintain the
original type without force `load_schema!` happened.
So I'd propose a way to allow lazy construct attribute type after
`load_schema!`, it makes us achieve the purpose.
```ruby
module Mod
included do
attribute :reserved_column, default: SENTINEL_VALUE do |name|
type_for_attribute(name)
end
end
end
```
|
How about reusing the database type in diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb
index dc3cfed88f..79e3b685e6 100644
--- a/activerecord/lib/active_record/attributes.rb
+++ b/activerecord/lib/active_record/attributes.rb
@@ -205,7 +205,7 @@ module ClassMethods
# tracking is performed. The methods +changed?+ and +changed_in_place?+
# will be called from ActiveModel::Dirty. See the documentation for those
# methods in ActiveModel::Type::Value for more details.
- def attribute(name, cast_type = Type::Value.new, **options)
+ def attribute(name, cast_type = nil, **options)
name = name.to_s
reload_schema_from_cache
@@ -246,6 +246,8 @@ def define_attribute(
def load_schema! # :nodoc:
super
attributes_to_define_after_schema_loads.each do |name, (type, options)|
+ type ||= type_for_attribute(name) || Type::Value.new
+
if type.is_a?(Symbol)
adapter_name = ActiveRecord::Type.adapter_name_from(self)
type = ActiveRecord::Type.lookup(type, **options.except(:default), adapter: adapter_name)I know this is a change in behaviour, so it would need a deprecation cycle, but the existing behaviour doesn't seem practically useful to me, and the deprecation would be easy to handle (pass |
|
Feel free to merge if you're not interested in my suggestion, by the way. This change is backwards-compatible, and #39820 shows that looking up the type with a block is useful in other places too. Ideally I don't think users should need to call |
|
Sorry for the delay, I had almost no time last week. I agree that the current behavior is not practically useful, so I've create an alternative #39830 to achieve the purpose. |
```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.
I've been reported this issue from a friend.
We'd like to define attribute by mixin, espetially to specify/overload
default value.
e.g.
Unfortunately, it will lose the original datetime precision.
I suggested using
define_attributeandtype_for_attribute(
define_attribute :attr, type_for_attribute(:attr), default: "..."),but it will force
load_schema!at mixin time.It means that there is no way that overload default but maintain the
original type without force
load_schema!happened.So I'd propose a way to allow lazy construct attribute type after
load_schema!, it makes us achieve the purpose.