Created
September 4, 2014 10:11
-
-
Save pawel2105/ae0435a4cf8675d53438 to your computer and use it in GitHub Desktop.
Reusable eager loading with rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Discussion < ActiveRecord::Base | |
scope :with_includes, -> { includes(:comments, :category) } | |
scope :by_recency, -> { order('created_at DESC') } | |
scope :visible, -> { where(hidden: false) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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