mislav/will_paginate

`raw_rnum_` being added to queried results and paged results wrong.

vinibol12 opened this issue · 3 comments

This issue started happening once I upgraded Rails from 6.0.3.7 to 6.1.5.
will_paginate 3.1.8

I have a query that looks for records and it's paginated.
When I do something like forms = Form.paginate(:page => page).order('forms.id desc') if I inspect the returned items I notice that raw_rnum_ is added to each record. I suppose this is will_paginate's doing as that attr doesn't exist in my db. will_paginate seems to use that when building paged queries. The value in each record is a sequential integer. So if the query returns three records we'll have the values 1, 2 and 3 as that attribute's values.
forms.map(&:raw_rnum_) #=> [1, 2, 3)

When the other bits of the query are put together I end up with a longer query and a condition at the end that tells which records to load (I assume based on the page size)

...AND "FORMS"."ID" IN (1,2,3) ORDER BY forms.id desc

This is causing my query to fail, since the actual ids of the items are not 1, 2 and 3.
This only happens if I upgrade rails as mentioned. Tried upgrading will_paginate to its latest, but the problem remains.
Before the upgrade of rails the SQL produced was the same. Except that in that case the form ids IN (...) was correct with the actual ids of the returned records and the values were obfuscated from the SQL.

...AND "FORMS"."ID" IN (:a4, :a5, :a6) ORDER BY forms.id desc ...["id", 22], ["id", 21], ["id", 1]]

The issue happens when I further my query to something my complex. Then the records are no longer returned.

The actual query object seems to built correctly, the count attribute in the query object seems correct, but the actual results don't return correctly.

Any ideas?

Hi, sorry that you're having trouble, but this doesn't sound like an issue caused by will_paginate. This library does not add any attribute called raw_rnum_ to your records. Also note that will_paginate does not actually construct your SQL queries: Active Record does, so you might have been affected by the ActiveRecord upgrade, not how will_paginate works with it.

@vinibol12 will_paginate library performs pagination by adding LIMIT and OFFSET clauses (or equivalents, depending on the underlying database engine) to the SQL query using ActiveRecord. It might be possible that Active Record generates wrong queries with LIMIT and OFFSET in the Oracle adapter.