Sunspot rails: include associated models when calling .results
Asked Answered
D

3

16

Let's say that I want to search for Events in my app. When I display the results, I want to display who created the event, for instance.

Is there any way to add .includes(:user) somewhere, to avoid unnecessary queries (one for each event)? I can't find it in the doc. Should I just index the user name with the event? But I'd have to keep the user info up to date...

Thanks

Deanedeaner answered 26/11, 2011 at 23:11 Comment(0)
D
25

Found the answer, it was actually quite simple:

Event.search(:include => [:user]) do...
Deanedeaner answered 28/11, 2011 at 19:22 Comment(0)
F
7

This is an updated answer, if We need to include an nested association

Then use this

Event.search(:include => {:user => :city})

In this case it will include user and his associated city

Federal answered 7/1, 2014 at 11:13 Comment(0)
O
1

Just in case you need to add a normal and nested include, here's what I needed:

Professional.search(include: [:user, {branches: [:business]}]) do 
Outandout answered 10/8, 2015 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.