diff options
author | Matthew Erhard <merhard@gmail.com> | 2016-05-11 13:09:34 -0400 |
---|---|---|
committer | Matthew Erhard <merhard@gmail.com> | 2016-05-11 13:21:01 -0400 |
commit | 556e530da41dce5ae8070e8e075390bbedb949c0 (patch) | |
tree | 356ccae535f0a627119a11a8e1cd23f6be829a13 /activerecord/test | |
parent | 59d252196b36f6afaafd231756d69ea21537cf5d (diff) | |
download | rails-556e530da41dce5ae8070e8e075390bbedb949c0.tar.gz rails-556e530da41dce5ae8070e8e075390bbedb949c0.tar.bz2 rails-556e530da41dce5ae8070e8e075390bbedb949c0.zip |
Define ActiveRecord::Attribute::Null#type_cast
Using ActiveRecord::Base.attribute to declare an attribute with a default value on a model where the attribute is not backed by the database would raise a NotImplementedError when model.save is called.
The error originates from https://github.com/rails/rails/blob/59d252196b36f6afaafd231756d69ea21537cf5d/activerecord/lib/active_record/attribute.rb#L84.
This is called from https://github.com/rails/rails/blob/59d252196b36f6afaafd231756d69ea21537cf5d/activerecord/lib/active_record/attribute.rb#L46 on an ActiveRecord::Attribute::Null object.
This commit corrects the behavior by implementing ActiveRecord::Attribute::Null#type_cast.
With ActiveRecord::Attribute::Null#type_cast defined, ActiveRecord::Attribute::Null#value (https://github.com/rails/rails/blob/59d252196b36f6afaafd231756d69ea21537cf5d/activerecord/lib/active_record/attribute.rb#L173..L175) can be replaced with its super method (https://github.com/rails/rails/blob/59d252196b36f6afaafd231756d69ea21537cf5d/activerecord/lib/active_record/attribute.rb#L36..L40).
fixes #24979
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/attributes_test.rb | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb index 2bebbfa205..48ba7a63d5 100644 --- a/activerecord/test/cases/attributes_test.rb +++ b/activerecord/test/cases/attributes_test.rb @@ -63,6 +63,15 @@ module ActiveRecord end end + test "model with nonexistent attribute with default value can be saved" do + klass = Class.new(OverloadedType) do + attribute :non_existent_string_with_default, :string, default: 'nonexistent' + end + + model = klass.new + assert model.save + end + test "changing defaults" do data = OverloadedType.new unoverloaded_data = UnoverloadedType.new |