JSON & RAILS

16 Sep 2018

There are a few ways to do this: to_json, fast_jsonapi is a new one, but my favorite is action_model_serializer. Why? Well, i spent many hours banging my head against other ways, using to_json, pouring over the documentation, following examples exactly and still couldnt get it, even resorted to manually serailizing. Then I found it, the one, active_model_serializer. Want to easily attach nested associations? Want to use one serializer for single models and associations? Want to pass in the exact same html from your show view? Here is how it works.

In Conclusion

I struggled for a while with to_json, which is still good and fast for simple direct associations OR methods. I never got it to work correctly with associations AND methods. It just didn’t seem as flexible, and it says on the rails API dock it may be depricated. Although, as of this writing, the active_record_serializers gem is still being developed, it’s working swimmingly so far. Thumbs up!