Created
October 22, 2016 04:25
-
-
Save 8parth/6b9418d726346e94359870c86db42ff2 to your computer and use it in GitHub Desktop.
to show how as_json can be overridden and used effectively
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
# 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