Permalink
Browse files

Make embed nil ids work

  • Loading branch information...
spastorino committed Sep 16, 2013
1 parent cad8faf commit dbf512c14c021aeab234e36ef7d1b8137ca8ce89
Showing with 13 additions and 1 deletion.
  1. +1 −1 lib/active_model/serializer.rb
  2. +12 −0 test/unit/active_model/serializer/has_one_test.rb
@@ -161,7 +161,7 @@ def serialize_ids(association)
if associated_data.respond_to?(:to_ary)
associated_data.map { |elem| elem.send(association.embed_key) }
else
- associated_data.send(association.embed_key)
+ associated_data.send(association.embed_key) if associated_data
end
end
@@ -54,6 +54,18 @@ def test_associations_embedding_objects_serialization_using_as_json
}, @user_serializer.as_json)
end
+ def test_associations_embedding_nil_ids_serialization_using_as_json
+ @user.instance_eval do
+ def profile
+ nil
+ end
+ end
+
+ assert_equal({
+ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil }
+ }, @user_serializer.as_json)
+ end
+
def test_associations_embedding_nil_objects_serialization_using_as_json
@association.embed = :objects
@user.instance_eval do

0 comments on commit dbf512c

Please sign in to comment.