diff --git a/CHANGELOG.md b/CHANGELOG.md index 3098b9431..6874babd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Features: Fixes: - [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh) +- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene) Misc: diff --git a/lib/active_model_serializers/model.rb b/lib/active_model_serializers/model.rb index 2e7908dfc..b9937cb5c 100644 --- a/lib/active_model_serializers/model.rb +++ b/lib/active_model_serializers/model.rb @@ -9,7 +9,7 @@ class Model attr_reader :attributes, :errors def initialize(attributes = {}) - @attributes = attributes + @attributes = attributes && attributes.symbolize_keys @errors = ActiveModel::Errors.new(self) super end diff --git a/test/active_model_serializers/model_test.rb b/test/active_model_serializers/model_test.rb index 8b9dd47d7..7bfb2edf4 100644 --- a/test/active_model_serializers/model_test.rb +++ b/test/active_model_serializers/model_test.rb @@ -7,5 +7,16 @@ class ModelTest < ActiveSupport::TestCase def setup @resource = ActiveModelSerializers::Model.new end + + def test_initialization_with_string_keys + klass = Class.new(ActiveModelSerializers::Model) do + attr_accessor :key + end + value = 'value' + + model_instance = klass.new('key' => value) + + assert_equal model_instance.read_attribute_for_serialization(:key), value + end end end