Skip to content

Instantly share code, notes, and snippets.

@jmorton
Forked from dhh/gist:981520
Created May 20, 2011 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmorton/983379 to your computer and use it in GitHub Desktop.
Save jmorton/983379 to your computer and use it in GitHub Desktop.
bulk api
# Bulk API design
#
# resources :posts
class PostsController < ApplicationController
before_filter :separate_bulk_ids
# GET /posts/1
# params[:id] => 1
# params[:ids] => [ 1 ]
#
# GET /posts/1,2,3,4
# params[:id] => 1
# params[:ids] => [ 1, 2, 3, 4 ]
#
def show
# Demonstrates how IDs are separated. Assigned to instance
# variables for testing purposes.
@id = params[:id]
@ids = params[:ids]
head :ok
end
# PUT /posts/1,2,3,4
def update
# Invalid id? No problem...
@posts = Post.all(:conditions => { :id => params[:ids] })
# optional: enforce policy on each post
@posts.each do |post|
post.update_attributes(params[:posts][post.id.to_s])
end
head :ok
end
protected
def separate_bulk_ids
if params[:id].present?
params[:ids] = params[:id].split(',')
params[:id] = params[:ids].first
end
end
end
@jmorton
Copy link
Author

jmorton commented May 20, 2011

I'm just noodling around and don't want to clutter the original gist with my comments quite yet...

Is it really necessary to have two separate actions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment