Rails Eager Load and Limit

后端 未结 1 1592
野趣味
野趣味 2020-12-19 10:12

I think I need something akin to a rails eager loaded query with a limit on it but I am having trouble finding a solution for that.

For the sake of simplicity, let

相关标签:
1条回答
  • 2020-12-19 10:46

    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
    0 讨论(0)
提交回复
热议问题