Skip to content

Commit

Permalink
Default nil associations to [] when embedding in root
Browse files Browse the repository at this point in the history
  • Loading branch information
joefiorini committed Feb 5, 2014
1 parent 9436d73 commit f8e9bd8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def embedded_in_root_associations
if hash.has_key?(key)
hash[key].concat(serialized_data).uniq!
else
hash[key] = serialized_data
hash[key] = serialized_data || []
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/unit/active_model/serializer/has_one_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ def profile
}, @user_serializer.as_json)
end

def test_associations_embedding_nil_ids_in_root_serialization_using_as_json
@association.embed = :ids
@association.embed_in_root = true
@user.instance_eval do
def profile
nil
end
end

assert_equal({
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil },
'profiles' => []
}, @user_serializer.as_json)
end

def test_associations_embedding_nil_objects_serialization_using_as_json
@association.embed = :objects
@user.instance_eval do
Expand Down

0 comments on commit f8e9bd8

Please sign in to comment.