Skip to content

Instantly share code, notes, and snippets.

@DanielAmah
Created May 29, 2020 17:09
Show Gist options
  • Save DanielAmah/43ca2022ae46f764bfd8054bad977591 to your computer and use it in GitHub Desktop.
Save DanielAmah/43ca2022ae46f764bfd8054bad977591 to your computer and use it in GitHub Desktop.
## We can leverage on eager loading using joins to load the plans with the subscription in one single query.
def index
@subscriptions = Subscription.joins(:plan).order(created_at: :desc).page(params[:page]).per(100)
end
## As a sidenote, I would move the logic for the order into a scope in the Subscription model
## Something like this
class Subscription < ApplicationRecord
scope :latest, -> { order(created_at: :desc) }
end
## In the controller
def index
@subscriptions = Subscription.joins(:plan).latest.page(params[:page]).per(100)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment