Skip to content

Instantly share code, notes, and snippets.

@mrbrdo
Last active August 29, 2015 14:10
Show Gist options
  • Save mrbrdo/aaa36b3490ad4e4ceec4 to your computer and use it in GitHub Desktop.
Save mrbrdo/aaa36b3490ad4e4ceec4 to your computer and use it in GitHub Desktop.
class Commit < Sequel::Model
end
class Repository < Sequel::Model
one_to_many :recent_commits, :class => 'Commit',
eager_limit_strategy: true,
order: Sequel.desc(:id),
limit: 3
end
# This will return all repositories with the last 3 commits loaded for each:
# Repository.eager(:recent_commits).all
# Queries generated on Postgres:
# SELECT * FROM "repositories"
# SELECT * FROM (
# SELECT *,
# row_number() OVER (PARTITION BY "commits"."repository_id" ORDER BY "id" DESC) AS "x_sequel_row_number_x"
# FROM "commits" WHERE ("commits"."repository_id" IN (1))
# ) AS "t1" WHERE ("x_sequel_row_number_x" <= 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment