1

I'm trying to set up a JSON object that I can make a clone of, so I need Jbuilder to list all object attributes and associations. I've done plenty of pick and choose JSON structures using Jbuilder, but can't seem to find a simple way to just list everything in one swoop without explicitly declaring it all.

This works fine for individual company attributes and all associated location attributes...

//show.json.jbuilder

company = @company

json.company do
  json.(company, :id)
  json.locations_attributes company.locations
end

But I'd like to include ALL the company attributes and can't seem to do it without listing them all json.(company, :id, :name, :line_of_business) etc.

Travis Smith
  • 622
  • 5
  • 22

1 Answers1

1

Well, you can use ActiveRecord #attributes method to look neat as

company = @company

json.company do
  json.(company, *company.attributes.keys)
  json.locations_attributes company.locations
end

Or as mentioned in this answer:

json.merge! company.attributes
Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36