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.
Asked
Active
Viewed 41 times
0

JohnGood
- 15
- 4
2 Answers
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
-
sorry missed the order part when copying from code editor – JohnGood Dec 01 '15 at 10:48
-
so that's exactly the query I'm doing, it works well but not with kaminari – JohnGood Dec 01 '15 at 10:57
-
with update 1, I get correct records for the first page, but for the second i get two first records from first page and then the last one (correct one) – JohnGood Dec 01 '15 at 11:07
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