Eager Load for Samples of Data to Count Relations in Rails
Sometimes you might want to load samples (shuffled) of a related model that matches certain criteria. For hitchi.de I wanted to display all Cities that have offers in the next to days. In order to limit the query load I used Rails’ includes() method and hacked it a little to best match my needs.
If you need to count relations more often or you can expect your data to be huge, better make use of Rails’ built-in :counter_cache