-
-
Save jmorton/983379 to your computer and use it in GitHub Desktop.
bulk api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?