diff options
author | Yuki Nishijima <mail@yukinishijima.net> | 2014-10-15 17:32:35 -0700 |
---|---|---|
committer | Yuki Nishijima <mail@yukinishijima.net> | 2014-10-15 17:32:35 -0700 |
commit | 074880c4eab3121025840998e587753203f65f6f (patch) | |
tree | dc5d290bda20162a773c38049a9acb5dd68eb091 /activerecord | |
parent | e01f1b3406057d48ee2742a80e909821cb8e5d43 (diff) | |
download | rails-074880c4eab3121025840998e587753203f65f6f.tar.gz rails-074880c4eab3121025840998e587753203f65f6f.tar.bz2 rails-074880c4eab3121025840998e587753203f65f6f.zip |
AR::UnknownAttributeError should include the class name of a record
This would be helpful if 2 models have an attribute that has a similar
name to the other. e.g:
before:
User.new(name: "Yuki Nishijima", projects_attributes: [name: "kaminari"])
# => ActiveRecord::UnknownAttributeError: unknown attribute: name
after:
User.new(name: "Yuki Nishijima", projects_attributes: [name: "kaminari"])
# => ActiveRecord::UnknownAttributeError: unknown attribute on User: name
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/errors.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_methods_test.rb | 6 |
3 files changed, 14 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index b12d048169..a1bef4aa93 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,13 @@ +* `AR::UnknownAttributeError` now includes the class name of a record. + + This would be helpful if 2 models have an attribute that has a similar + name to the other one. e.g.: + + User.new(name: "Yuki Nishijima", project_attributes: {name: "kaminari"}) + # => ActiveRecord::UnknownAttributeError: unknown attribute on User: name + + *Yuki Nishijima* + * Fix regression causing `after_create` callbacks to run before associated records are autosaved. diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index 52c70977ef..2b9b3c8f73 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -167,7 +167,7 @@ module ActiveRecord def initialize(record, attribute) @record = record @attribute = attribute.to_s - super("unknown attribute: #{attribute}") + super("unknown attribute on #{@record.class}: #{attribute}") end end diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 7ca7349528..3fdb70a137 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -736,11 +736,11 @@ class AttributeMethodsTest < ActiveRecord::TestCase def test_bulk_update_raise_unknown_attribute_error error = assert_raises(ActiveRecord::UnknownAttributeError) { - @target.new(:hello => "world") + Topic.new(:hello => "world") } - assert_instance_of @target, error.record + assert_instance_of Topic, error.record assert_equal "hello", error.attribute - assert_equal "unknown attribute: hello", error.message + assert_equal "unknown attribute on Topic: hello", error.message end def test_methods_override_in_multi_level_subclass |