ruby on rails - Belongs_to association in ActiveModel::Serializer removes root from JSON -


I currently write an API with rail. After seeing RailsCast about ActiveModel :: Serializer I wanted to try them out.

A brief overview on my current models:

  class clubs & lt; ActiveRecord :: Base has_many: User and Class User & lt; ActiveRecord :: Base is valid: Username, Attendance: TrueSat_to: Club End   

The following JSON returns from the user:

  {"id": 1, "username": "test", "first name": "test", "last name": "test", "created_at": "2014-10- 10T12: 42: 12.186Z", "updated_at": "Test", "Mail": "Test", "Club_ID": 1}  < / Pre> 

As an example, I have implemented UserSerializer as the following from the ActiveModel :: Serializer page:

  class UserSerializer & lt; ActiveModel :: Serializer Features: ID ,: Username ,: First Name, Last Name :: Mail ,: Street,: City,: club_id end   

This "user" adds: the root As my JSON format. In addition, for example

  add_to: club   

shows the serializer. But if I add related, then I do not get "user": as the root of my json Is it intentional behavior? Is there a way to change this? After additional testing it seems that "user": Root element randomly - you have not yet got a pattern - added or dropped.

I tried to add the root: for my users_controller I came to know that to solve the problem , But I still want to make an explanation if someone knows what's wrong with the concerned.

The_On serves related functions and adds "user": as JSON root.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -