Skip to content

Instantly share code, notes, and snippets.

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