bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Order error with batches #316

Open lukasedw opened 1 year ago

lukasedw commented 1 year ago

I am using this gem API to build a datatable endpoint on graphql. While testing some things I saw that this was not working:

::Chat::IndexGrid.new(order: "name", descending: false).data_hash

It was not ordered. Then I started to debug, I notice the condition on lib/datagrid/columns.rb:490, when using the batch each it is not considered the order of the asset. I can debug more later, but for now, thats what I have.

A workaround is set self.batch_size = 0.

bogdan commented 1 year ago

Can you show the grid source code?

lukasedw commented 1 year ago

Sorry, it was my fault.

bogdan commented 1 year ago

I actually can reproduce this issue. The order is really ignored when using batches. It seems like this is a rails batches design that can not be turned off:

https://github.com/rails/rails/blob/311d0babfbacd105c2452339e9037e4a50400183/activerecord/lib/active_record/relation/batches.rb#L292

I think it is bad: Rails should allow a custom order when using batches. I don't see any way to override this behaviour on the datagrid level within rails (even with monkey patch/power hacking).

So, currently the only idea is submit a PR into rails that would allow to keep relation order.

bogdan commented 1 year ago

@zhuravel, I would want to know your opinion on the subject as I believe you are extensively using datagrid with batches in your project.

Rails basically ignores the relation order when using batches: https://github.com/rails/rails/blob/311d0babfbacd105c2452339e9037e4a50400183/activerecord/lib/active_record/relation/batches.rb#L292

Did you notice that behaviour? Does it cause issues for you? Do you have a workaround or best practice?