model定義
class Post < ApplicationRecord
has_many :latest_comments, -> { limit 10 }, class_name: "Comment", foreign_key: :post_id
end
Postからlatest_commentsを取得
Post.all.each {|post| p post.latest_comments }
Post.includes(:latest_comments).each {|post| p post.latest_comments }
Post.eager_load(:latest_comments).each {|post| p post.latest_comments }
上記の3つの結果が異なります。原因はincludes、eager_loadがlimitをサポートできないからです。
Post.includes(:latest_comments).first.latest_comments
Post.includes(:latest_comments).second.latest_comments
一個のpostだけ、取得する場合は、問題ないです。