Pagination breaks when I add a join and group statement to a query?

308 Views Asked by At

I have an index page with Subscriptions. A Subscription has_many SubscriptionVersions and I need to order them based on the latest SubscriptionVersions authorized_at date.

This is my controller action

  def index
    @subscriptions = current_account.subscriptions
      .includes(:plan, :user)
      .joins("INNER JOIN subscription_versions
        ON subscription_versions.subscription_id = subscriptions.id")
      .group("plans.id, subscriptions.id, users.id")
      .order("MAX(subscription_versions.authorized_at) ASC")
      .paginate(page: params[:page], per_page: 20)
  end

Removing the join and order statements, pagination works fine, but if I add them the results are not paginated.

What is going on and why?

0

There are 0 best solutions below