Skip to content

Instantly share code, notes, and snippets.

@pawel2105
Created September 4, 2014 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawel2105/ae0435a4cf8675d53438 to your computer and use it in GitHub Desktop.
Save pawel2105/ae0435a4cf8675d53438 to your computer and use it in GitHub Desktop.
Reusable eager loading with rails
class Discussion < ActiveRecord::Base
scope :with_includes, -> { includes(:comments, :category) }
scope :by_recency, -> { order('created_at DESC') }
scope :visible, -> { where(hidden: false) }
end
class DiscussionsController < ApplicationController
def index
@discussions = Discussion.with_includes.visible.by_recency
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment