I am using Rails 4.2. I have 3 tables:
class Collection < ActiveRecord::Base has_many :shares has_many :images, through: :shares has_many :latest_images, -> { order(created_at: :desc).limit(10) }, class_name: 'Image', through: :shares, source: :image end class Share < ActiveRecord::Base belongs_to :image belongs_to :collection end class Image < ActiveRecord::Base has_many :shares has_many :collections, through: :shares end
My goal is to select some collections and preload the first 10 latest maps of each collection using the latest_images relation.
If I just:
collections = Collection.where(some_condition).includes(:latest_images)
Problem: last_images will contain all the cards, not just the last 10 (even if there is limit(10) )
collections.first.latest_images.count
Instead, if I add limit(10) after loading the collections, I will have a problem with requesting N + 1:
collections.each { |collection| collection.latest_images.limit(10).do_something }
Any solution?
Progm source share