smashingboxes / box_cutter

Box Cutter is inspired by thoughtbot's Suspenders. Just like Suspenders says, use Box Cutter if you're in a rush to build something amazing; don't use it if you like missing deadlines.
MIT License
4 stars 2 forks source link

ActiveModel Serializers? #10

Open joeyjoejoejr opened 10 years ago

joeyjoejoejr commented 10 years ago

I think that this is a fantastic way of formalizing serialization logic. I think it should be included by default.

https://github.com/rails-api/active_model_serializers

leonelgalan commented 10 years ago

Having use both Jbuilder and ActiveModel Serializers to build json responses, I like Jbuilder more. Jbuilder it's more explicit and ActiveModel Serializers associations (has_many :foo) are a hot spot for N+1 queries. With "actual_id" we have some nested serializers (4 or 5 levels) that were killing our performance.

joeyjoejoejr commented 10 years ago

I don't think Jbuilder solves the n+1 problem. The associations in amy, literally call the model's association, like you would if you were going to embed a model in Jbuilder. It comes down to being diligent with eager loading wether you use jbuilder or ams. I just prefer working with objects over templates in general.

joeyjoejoejr commented 10 years ago

For details on how to deal with that issue: http://stackoverflow.com/questions/18134649/eager-load-associations-with-active-model-serializers

RickCarlino commented 10 years ago

I prefer ActiveModel serializers, and it seems like rails 4 seems to be baking them into the codebase more and more. Support has only gotten better.

That being said, they both do an acceptable job. AMS are a preference, but using Jbuilder isn't bad, either.

BrandonMathis commented 10 years ago

The trend that I have seen lately is towards ActiveRecord Serializers and I think that is not without reason. I've use Activemodel Serializers in the past on several projects that use ember-data and they two work great together. I have no experience regarding angular but I cant speak to that.

What I like most about ActiveModel Serializer is that is separates the code for building json into a separate 'serializers' dir. I feel that it is cleaner. Any time I've worked on a project that uses JBuilder, coders just drop the JBuilder code directly into the model resulting in 200+ line classes where 80% of that code is dedicated towards building a fancy json structure that is unique to the project.

With ActiveModel Serializers, the JSON is typically similar from app to app.

LNauman commented 10 years ago

I saw this in an article of thoughtbot's and thought it was relevant.

"If the JSON you’re emitting is hairy enough to merit the use of actual templates, you might want something like Jbuilder or RABL. But before diving into that world, consider the underrated activemodelserializers. It’s easy to use and doesn’t add much complexity over the old-and-busted approach – you just specify the attributes and associations to include in a different way."

elfassy commented 10 years ago

My main problem with activemodelserializer is that it doesn't support versioning. This is a must for example with supporting multiple version of an ios app. Api/v1/path I strongly advise against it for this reason. My vote goes for jbuilder On Aug 8, 2014 11:07 AM, "Lindsay" notifications@github.com wrote:

I saw this in an article http://robots.thoughtbot.com/better-serialization-less-as-json of thoughtbot's and thought it was relevant.

"If the JSON you’re emitting is hairy enough to merit the use of actual templates, you might want something like Jbuilder or RABL. But before diving into that world, consider the underrated activemodelserializers. It’s easy to use and doesn’t add much complexity over the old-and-busted approach – you just specify the attributes and associations to include in a different way."

— Reply to this email directly or view it on GitHub https://github.com/smashingboxes/box_cutter/issues/10#issuecomment-51614330 .

joeyjoejoejr commented 10 years ago

@elfassy, I hadn't really thought much about that although you could just specify custom serializers in your associations.

From the docs:

  has_many :comments, serializer: CommentShortSerializer
elfassy commented 10 years ago

@joeyjoejoejr Not sure how that would work if you need multiple Serializers for each version. https://github.com/rails-api/active_model_serializers/issues/144

elfassy commented 9 years ago

This pull requests makes custom serializers on associations possible in AMS: https://github.com/rails-api/active_model_serializers/commit/9f9715801ab7c8035688d99b458ddff17e58c5b2 That should solve the versioning problem. For now my vote still goes for jbuilder + Decorator + versioncake, but that might change with Rails 5 which is said to have better support for AMS