Skip to content

Conversation

@nibble
Copy link

@nibble nibble commented Jan 21, 2014

Sequel can preload all matching associations by using the eager method. It will first load the dataset with the given query, and then it will query the database once for each one of the specified associations. For this to work, the query executing method has to be all, not the usual each, but it can still have pagination parameters as part of the query.

This change provides an all method to the pager, that is propagated to Sequel's dataset. It also includes ArrayPager#all, that returns the array range, to ensure proper compatibility with plain arrays.

Here is an usage example. It will generate 2 SQL queries, instead of 1 + N:

# controller
@artists = paginate(Artist.eager(:albums))
<!-- view -->
<?r @artists.all.each do |artist| ?>
  #{artist.name}
  <?r artist.albums.each do |album| ?>
    #{album.title}
  <?r end ?>
<?r end ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant