ActiveRecord :includes - how to use map with loaded associations?

孤街浪徒 提交于 2019-12-04 11:48:47

pluck should always make a new query to database. Not sure why you think it does not happen in an each loop. Maybe you did not see the log because it is in between your prints?

There are 2 possibilities how to avoid additional queries.

  1. Since orders are already loaded because you include them, you can do admins.map {|a| [a.name, a.orders.collect(&:operation)]}

  2. Using joins (see @tihom's comment).

Edit: I just tested the each/ map behavior and it reloads every time as expected.

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