Sunspot rails: include associated models when calling .results

老子叫甜甜 提交于 2019-11-30 22:09:49

问题


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


回答1:


Found the answer, it was actually quite simple:

Event.search(:include => [:user]) do...



回答2:


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




回答3:


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

Professional.search(include: [:user, {branches: [:business]}]) do 


来源:https://stackoverflow.com/questions/8282344/sunspot-rails-include-associated-models-when-calling-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!