Rails Eager Load and Limit

寵の児 提交于 2019-11-29 10:36:50

Regardless of what "that article" said, the issue is in SQL you can't narrow down the second sql query (of eager loading) the way you want in this scenario, purely by using a standard LIMIT

You can, however, add a new column and perform a WHERE clause instead

  1. Change your second association to Person has_many :sample_of_comments, conditions: { is_sample: true }
  2. Add a is_sample column to comments table
  3. Add a Comment#before_create hook that assigns is_sample = person.sample_of_comments.count < 5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!