Skip to content

Instantly share code, notes, and snippets.

@searls
Created October 22, 2011 23:59
Show Gist options
  • Save searls/1306652 to your computer and use it in GitHub Desktop.
Save searls/1306652 to your computer and use it in GitHub Desktop.
class Camel < ActiveRecord::Base
def initialize(attributes = nil, options = {})
super(underscore_keys(attributes), options)
end
def attributes=(new_attributes, guard_protected_attributes=nil)
super(underscore_keys(new_attributes, guard_protected_attributes))
end
def serializable_hash(options = nil)
Hash[super(options).map { |k,v| [k.to_s.camelize.uncapitalize,v] }]
end
private
def underscore_keys(hash)
Hash[hash.map { |k,v| [k.to_s.underscore.to_sym,v] }]
end
end
class CamelController < ApplicationController
respond_to :json
def index
respond_with Camel.all
end
def create
@camel = Camel.new(params[:camel])
if @camel.save
respond_with @camel, status: :created, location: @camel
else
respond_with @camel.errors, status: :unprocessable_entity
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment