Falcon
Falcon

Reputation: 1463

Rails JSON auto include associations

In my controller I must render some data with a lot of associations.

It look like this:

render :json => current_client.items.some_scopes.includes(:assoscitation1, :assoscitation2, :assoscitation3 => {:nested_assoscitation1 => :nested_assoscitation2}).to_json(
        :include => {:assoscitation2 => {:include => :type}, 
                     :assoscitation1 => {}, 
                     :assoscitation3 => {:include => 
                       {:product => {:include => :type}}
                      }
                    }
    )

AR includes is fine. I added it in scope. But specify all this associations in to_json method again makes me sad.
How can i remove this duplication?
Or maybe add presentrs level?
Thanks.

Upvotes: 1

Views: 405

Answers (2)

Brian
Brian

Reputation: 31292

RABL is awesome while AMS is also a good option:

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

Upvotes: 0

varatis
varatis

Reputation: 14750

At this point (of ugliness in big hashes) I would recommend RABL.

https://github.com/nesquena/rabl/

Upvotes: 2

Related Questions