Using active model serializers for json and csv in Rails?

1.7k Views Asked by At

I'm using active model serializers in my rails app for json, which is working great. Now I also need to export that data as csv and have had to rewrite all of the methods in the serializer for the csv method.

Is there anyway to take the serialized json and convert it to csv? Or a way to share code easily between the model and serializer?

2

There are 2 best solutions below

1
On

I've never tried it - but there is the as_csv gem.

Behind the scenes, any classes that include ActiveModel::Serialization will expose their attributes with to_csv.

0
On

Just for others searching, this is now part of ActiveModel