Skip to content

Instantly share code, notes, and snippets.

@8parth
Created October 22, 2016 04:25
Show Gist options
  • Save 8parth/6b9418d726346e94359870c86db42ff2 to your computer and use it in GitHub Desktop.
Save 8parth/6b9418d726346e94359870c86db42ff2 to your computer and use it in GitHub Desktop.
to show how as_json can be overridden and used effectively
# override as_json method of User class
def as_json(options={})
super(only: [:picture, :age],
methods: [:name])
end
# put helper methods, such as creating full name from given first_name and last_name attribute of user
def name
self.first_name +" " +self.last_name
end
# example response, note that timestamps, authentication token and password digest fields are not present
# user: {
# name: "some name",
# picture: "url",
# age: 20
# }
# most important: calling render json: @user returns this response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment