How to eager load assocations

I have the following associations in my ActiveRecord models

  1. Groups

  2. Users

Each Group has and belongs to many users

There is an api endpoint - /api/v1/groups which returns a list of the groups. In the controller when I am sending a list of 20 groups, the Users property is not populated beforehand in the json structure. Anyway to include the Users property in the json response.

Thanks,

Ganesh

You can use Group.includes(:users)

See here more info
http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations

​Includes didn't work. But I asked on IRC and someone suggested using a
custom serializer.

That worked like a charm