Include nested associations in JSON
NickName:baconck Ask DateTime:2016-11-07T05:33:22

Include nested associations in JSON

# Person.rb
class Person < ActiveRecord::Base
  has_many :events
end

# Event.rb
class Event < ActiveRecord::Base
  belongs_to :person
  has_many :tables
end

# Table.rb
class Table < ActiveRecord::Base
  belongs_to :event
end

Within Person.rb I am trying to create a method to get all events and tables in one query

def complete_events
  events.includes(:tables)
end

I can see in the console it is loading both events and table

  Event Load (1.1ms)  SELECT "events".* FROM "events" WHERE "event"."person_id" = $1  [["person_id", 17]]
  Table Load (0.5ms)  SELECT "tables".* FROM "tables" WHERE "tables"."event_id" IN (10)

However, the returned object is only the event records.

#<ActiveRecord::Relation [#<Event id: 1, name: "some event">]

How can I get the returned record to nested like below?

#<ActiveRecord::Relation [#<Event id: 1, name: "some event", tables: [id: 1, seats: 4, id: 2, seats: 4]>]

Edit

I am able to iterate and create a all_events object, but it not nested.

  def complete_events
    all_events = []
    events.includes(:tables).each do |event|
      all_events << event
      all_events << event.tables
    end
    all_events
  end

Copyright Notice:Content Author:「baconck」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40454785/include-nested-associations-in-json

Answers
potashin 2016-11-06T22:23:12

Use as_json:\n\ndef complete_events\n as_json(\n include: {\n events: {\n include: tables\n }\n )\nend\n",


More about “Include nested associations in JSON” related questions

How to include nested and sibling associations in active record to_json?

I have a Course model with 2 associations to another model, Tree: belongs_to :interaction_outline, :class_name =&gt; "Tree", :foreign_key =&gt; "

Show Detail

Rails JSON auto include associations

In my controller I must render some data with a lot of associations. It look like this: render :json =&gt; current_client.items.some_scopes.includes(:assoscitation1, :assoscitation2, :assoscitati...

Show Detail

Include IDs of nested associations in ActiveModel serializers

In some cases nested associations are embedded in the JSON, in others they are not. So far so good, that behaves the way I want. But I want that in the cases where they aren't embedded the IDs of the

Show Detail

How to include all of my child associations in my rails api json response

I'm trying to output my entire object as a json, but it isn't rendering the associations. My object is loaded using includes like this: School.includes(head_office: [class_rooms: [desks: [:chair...

Show Detail

sequelize oneToMany create nested associations

Is there a way to have sequelize create nested associations via a single create call? For example if I have a model Data that hasMany of model User, and each user hasMany of model Permission. Is th...

Show Detail

ActiveModel::Serializers JSON API nested associations include?

I haven't found the exactly same question on Stackoverflow. Besides AMS changes so rapidly, that even 2-year-old answers get outdated often. I use Rails 5 API-only and the gem 'active_model_serial...

Show Detail

Include model associations from within the model

I need to clean up my code from using: # class ProjectsController &lt; ApiController def show render json: Project.find_by(id: params[:id]).as_json(include: :user) end to: render json: Project.

Show Detail

Rails 4 to_json include all associations

so I know how to include a specific association: obj.to_json include: :some_association_name What I'm trying to figure out is if there's a way to blindly include all associations and not have to ...

Show Detail

Sencha Touch nested JSON - Store load with Associations

I want to load nested JSON in a Store, but the data is not correctly mapped. No problem with a single regModel but I can´t get the associations to work. // JSON in test.json {"message" : { "

Show Detail

Render nested associations as json

Below are my model definitions # GoogleApp model class GoogleApp &lt; ApplicationRecord has_many :gmail_users, foreign_key: 'app_account', primary_key: 'google_account', dependent: :destroy has...

Show Detail