Skip to content

Renaming an attribute

fabrik42 edited this page Apr 20, 2011 · 5 revisions

Renaming an attribute

When calling a method, association or whatever you want to add to the template, you always can change the name of the attribute that will be rendered in the response, by passing an :as option.

class User < ActiveRecord::Base

  acts_as_api

  api_accessible :public do |t|
    t.add :first_name
    t.add :last_name, :as => :family_name
  end

end

Which would result in a JSON response like this:

{
  "user": {
    "first_name": "Luke",
    "family_name": "Skywalker"
  }
}