rails/rails — pull request #14022 by semenyukdmitriy (2014-02-12 02:35:59+00:00)
Title: Load included associations with limit if any

```ruby
class User < ActiveRecord::Base
has_many :tweets
has_many :recent_tweets, ->{ limit(3) }, class_name: "Tweet"
end

class Tweet < ActiveRecord::Base
belongs_to :user
end

user = User.create!(name: "John Appleseed")
user.tweets.create!(5000.times.map { |num| {text: "tweet-##{num}"} })
```

Loading all users with just recent tweets may be really slow — `User.includes(:recent_tweets).load` produces these queries:

* `SELECT "users".* FROM "users"`
* `SELECT "tweets".* FROM "tweets" WHERE "tweets"."user_id" IN (1)`

Pay attention to second query: it doesn't have `LIMIT`. And now imagine if user has gazillion tweets. :)