0

I have two tables, authors and books, books have a publishing date, I would like to order authors by the date of their last published book, I was doing this includes(:books).where(blablabla).order('books.published_date DESC') but it breaks kaminari's pagination (8 records per page, 9 to be displayed), I get correct records on the first page, but on the second page I get first two records from the first page plus correct record.

JohnGood
  • 15
  • 4

2 Answers2

0

Try this:-

Author.includes(:books).where(blablabla).order('books.published_date DESC')

Update 1:-

@authors = Author.includes(:books).where(blablabla).order('books.published_date DESC')
@authors = @authors.page(params[:page)

Hope this may helpful!!

user3506853
  • 814
  • 5
  • 3
0

You may want to have a look at Yap. You can define an alias for books.published_date and sort by that alias. See the example.

User.joins(:team).paginate(sort: {team: :asc}) # would sort by team.name

Use .includes(:team) if you also need to access any attributes of it to use eager loading.

AmShaegar
  • 1,491
  • 9
  • 18