Reputation: 1333
I have a Rails view that makes heavy use of scopes to filter down an Invoice table of hundreds of thousands of rows down to several thousand @invoices
filtered records. @invoices
object is an ActiveRecord relation.
Once the user presses a button in the view, these thousands of records need to be sent for processing to another controller / model.
Which would be the best way to accomplish this?
Passing the @invoices
object as a param is not possible, so I can only think of two options:
1) passing an array of ids as parameters like this:
link_to bulk_process_path(@comprobantes.pluck(:id)), method: :post
but I'm worried I would hit the server's post max size if there a lot of records
2) passing the scopes involved in the original filtered view as parameters and recreating the filter in the target controller.
However this seems like unnecessary hits on the database. Furthermore, if I ever wanted to implement checkboxes to further refine the filtered view, then this method wouldn't work
3) Creating a temp table in the view, sending the name as a parameter and then reading it from the external controller? Then I'd have to keep track of and delete stale temp tables. Doesn't seem very elegant.
Maybe I'm missing something obvious but there doesn't seem to be an elegant solution.
Any help would be appreciated.
Upvotes: 0
Views: 927
Reputation: 15838
I can suggest another option.
When the user enters the page and starts filtering, you can save the filters on the session, then you do ajax requests on each checkbox changes and you can save those ids as exceptions when it's unchecked or remove the exception when it's checked.
You can even use websockets to make it more realtime.
You can also change the session storage method to ActiveRecordStore if you think the exceptions array can be too big, or use something like redis which is really fast.
That way, when the user has finished finteuning the filter, you do a post request but you don't need to send any params, everything is saved on the session. You then can exclude all the ids of unchecked and recreate the filter with the parameters.
Personally I think I would go this way. Hope this helps.
Upvotes: 1