mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Bugfix: include nested has_many association
Currently, doing `include: author.bio` would work correctly, but not for has_many associations such as `include: author.roles`. This fixes it. The problem was basically that we were not handling arrays for has_many linked, as happens for ArraySerializers.
This commit is contained in:
@@ -27,6 +27,7 @@ module ActiveModel
|
||||
def setup
|
||||
@author = Author.new(name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@author.roles = []
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||
@post.comments = [@comment]
|
||||
@@ -42,6 +43,7 @@ module ActiveModel
|
||||
def test_has_many
|
||||
assert_equal(
|
||||
{ posts: { type: :has_many, options: { embed: :ids } },
|
||||
roles: { type: :has_many, options: { embed: :ids } },
|
||||
bio: { type: :belongs_to, options: {} } },
|
||||
@author_serializer.class._associations
|
||||
)
|
||||
@@ -52,6 +54,9 @@ module ActiveModel
|
||||
elsif name == :bio
|
||||
assert_equal({}, options)
|
||||
assert_nil serializer
|
||||
elsif name == :roles
|
||||
assert_equal({embed: :ids}, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
else
|
||||
flunk "Unknown association: #{name}"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user