jbihan
jbihan

Reputation: 3099

Returning count of child in json.rabl

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 !

Upvotes: 1

Views: 714

Answers (1)

Bigxiang
Bigxiang

Reputation: 6702

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.

Upvotes: 4

Related Questions