Returning count of child in json.rabl

723 Views Asked by At

I have a Location model that I render in json format using Rabl. My index.json.rabl looks that way :

object false
collection @locations

attributes :id, :name, :address, :rating

:rating is an integer calculated from records in the Rating model (a Location has_many Rating, a Rating belongs_to a Location). But now I would like to retrieve also in the Rabl file the number of line of the Rating model used to calculate this value.

I tried :

child :ratings do
    attributes :count
end

and

node(:ratings_count) { |m| @ratings.count }

But obviously it doesn't work... Could anyone help me there ?

Thanks !

1

There are 1 best solutions below

1
On BEST ANSWER

If I don't misunderstand what you mean. I think you want to use rating.count in rabl.

You have closed to the answer using "node".

node(:ratings_count) { |l| l.ratings.count } 

In the block of node, local variable "l" is one object of the collection above.