# How to eager load assocations

**URL:** <https://www.ruby-forum.com/t/how-to-eager-load-assocations/240830>\
**Category:** Rails\
**Created:** [May 2, 2014, 10:02am UTC](https://www.ruby-forum.com/t/how-to-eager-load-assocations/240830 "2014-05-02T10:02:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [May 2, 2014, 10:02am UTC](https://www.ruby-forum.com/t/how-to-eager-load-assocations/240830/1 "2014-05-02T10:02:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ganesh\_Ranganathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9bcb4/32.png) [@Ganesh\_Ranganathan](https://www.ruby-forum.com/u/Ganesh_Ranganathan)\
**Post date:** [May 2, 2014, 11:24am UTC](https://www.ruby-forum.com/t/how-to-eager-load-assocations/240830/2 "2014-05-02T11:24:02Z")

</div>

You can use Group.includes(:users)

See here more info

> **[Active Record Query Interface — Ruby on Rails Guides](https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations)**
>
> Active Record Query InterfaceThis guide covers different ways to retrieve data from the database using Active Record.After reading this guide, you will know: How to find records using a variety of methods and conditions. How to specify the order,...

---

<div class="post-metadata">

**Author:** ![Ganesh\_Ranganathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9bcb4/32.png) [@Ganesh\_Ranganathan](https://www.ruby-forum.com/u/Ganesh_Ranganathan)\
**Post date:** [May 2, 2014, 11:47am UTC](https://www.ruby-forum.com/t/how-to-eager-load-assocations/240830/3 "2014-05-02T11:47:27Z")

</div>

On Fri, May 2, 2014 at 2:54 PM, Andrei G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
