I have this code in the controller:
<code> render json: item.item_tags.map do |item_tag|
{
tags_count: item_tag.tags.count,
other_count: item_tag.other_association.count,
tag: {
id: item_tag.tag.id,
name: item_tag.tag.name,
slug: item_tag.tag.slug,
things: ThingSerializer.new.index(item_tag.tag.things),
}
}
end
</code>
<code> render json: item.item_tags.map do |item_tag|
{
tags_count: item_tag.tags.count,
other_count: item_tag.other_association.count,
tag: {
id: item_tag.tag.id,
name: item_tag.tag.name,
slug: item_tag.tag.slug,
things: ThingSerializer.new.index(item_tag.tag.things),
}
}
end
</code>
render json: item.item_tags.map do |item_tag|
{
tags_count: item_tag.tags.count,
other_count: item_tag.other_association.count,
tag: {
id: item_tag.tag.id,
name: item_tag.tag.name,
slug: item_tag.tag.slug,
things: ThingSerializer.new.index(item_tag.tag.things),
}
}
end
ThingSerializer
here also has a map
which goes through and serializes all the passed things.
It’s supposed to return a list based on the map
value.
However, it returns a list of default item_tags
, i.e.:
<code>[{
"id": 236,
"status": "draft",
"position": 0,
"item_id": 234,
"tag_id": 123,
"inserted_at": "2024-03-19T10:23:56.000+03:00",
"updated_at": "2024-03-19T10:23:56.000+03:00"
}]
</code>
<code>[{
"id": 236,
"status": "draft",
"position": 0,
"item_id": 234,
"tag_id": 123,
"inserted_at": "2024-03-19T10:23:56.000+03:00",
"updated_at": "2024-03-19T10:23:56.000+03:00"
}]
</code>
[{
"id": 236,
"status": "draft",
"position": 0,
"item_id": 234,
"tag_id": 123,
"inserted_at": "2024-03-19T10:23:56.000+03:00",
"updated_at": "2024-03-19T10:23:56.000+03:00"
}]
But if I do this:
<code> actual_json = item.item_tags.map do |item_tag|
{
tags_count: item_tag.tags.count,
other_count: item_tag.other_association.count,
tag: {
id: item_tag.tag.id,
name: item_tag.tag.name,
slug: item_tag.tag.slug,
things: ThingSerializer.new.index(item_tag.tag.things),
}
}
end
render json: actual_json
</code>
<code> actual_json = item.item_tags.map do |item_tag|
{
tags_count: item_tag.tags.count,
other_count: item_tag.other_association.count,
tag: {
id: item_tag.tag.id,
name: item_tag.tag.name,
slug: item_tag.tag.slug,
things: ThingSerializer.new.index(item_tag.tag.things),
}
}
end
render json: actual_json
</code>
actual_json = item.item_tags.map do |item_tag|
{
tags_count: item_tag.tags.count,
other_count: item_tag.other_association.count,
tag: {
id: item_tag.tag.id,
name: item_tag.tag.name,
slug: item_tag.tag.slug,
things: ThingSerializer.new.index(item_tag.tag.things),
}
}
end
render json: actual_json
It works fine.
Why is it this way?
PS. I do similar things in other controllers and it works as expected. Only here it’s not returning the expected value.