user483040
user483040

Reputation:

Rails 4 to_json include all associations

so I know how to include a specific association:

obj.to_json include: :some_association_name

What I'm trying to figure out is if there's a way to blindly include all associations and not have to list each one individually. I optimistically tried:

obj.to_json include: :all

That didn't work, of course. Does anyone have any ideas?

thanks!

Upvotes: 1

Views: 636

Answers (1)

Uzbekjon
Uzbekjon

Reputation: 11813

Try this:

obj.to_json(include: obj.class.reflect_on_all_associations.map(&:name))

Upvotes: 6

Related Questions