How to eager load assocations

18 views
Skip to first unread message

Ganesh Ranganathan

unread,
May 2, 2014, 4:01:17 AM5/2/14
to chen...@googlegroups.com, rubyonra...@googlegroups.com
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 - <Site>/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

Andrei G.

unread,
May 2, 2014, 5:24:02 AM5/2/14
to rubyonra...@googlegroups.com

Ganesh Ranganathan

unread,
May 2, 2014, 5:45:56 AM5/2/14
to rubyonra...@googlegroups.com

On Fri, May 2, 2014 at 2:54 PM, Andrei G. <li...@ruby-forum.com> wrote:
You can use Group.includes(:users)

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

That worked like a charm
Reply all
Reply to author
Forward
0 new messages