Ask A Question

Notifications

You’re not receiving notifications from this thread.

Is there a "Rails" way to batch process API requests?

Brian Carpenter asked in Rails

I have a situation where I'm generating N post requests for given table in a vuejs app, but sending them to the rails controller one by one. I'd like to batch them all up in the app as a JSON object, then send one request over and let rails parse it out.

Would this be as simple as writing a custom controller action?

Wondering if rails has a framework/method for handling batch requests or if there's some standard approaches I should be using.

Thanks.

Reply

You're exactly right. If you send them over as an array, Rails can just loop through the array in the params and create them all one-by-one.

Then you just have to decide how you want to handle errors. Some cases you may want to cancel everything if one fails and return the error as JSON, or return JSON with two arrays of the successful and unsuccessful creates. Depends on what you need really.

Ryan Bates made a Railscast on this way back when I guess. http://railscasts.com/episodes/414-batch-api-requests I might have to do something similar in a future episode. 👍

Reply

Very helpful, thanks.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.