Skip to content

Instantly share code, notes, and snippets.

@Chryus
Last active August 26, 2016 20:37
Show Gist options
  • Save Chryus/3e1db6c6a76991ee7e6f9185785f8b12 to your computer and use it in GitHub Desktop.
Save Chryus/3e1db6c6a76991ee7e6f9185785f8b12 to your computer and use it in GitHub Desktop.
# customizing as_json with standalone JBuilder DSL
def as_json(options={})
Jbuilder.encode do |json|
graffito = self
json.(graffito, :incident_address, :borough, :latitude, :longitude)
json.upvotes(graffito.upvotes.count)
json.upvoted_by(graffito.upvotes.pluck(:user_id))
end
end
# Customizing as_json with plain old Rails
def as_json(options={})
graffito = super(:only => [:incident_address, :borough, :latitude, :longitude])
graffito[:upvotes] = upvotes.count
graffito[:voted_by] = upvotes.pluck(:user_id)
graffito
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment