27

I have three models:

class A < ActiveRecord::Base
  has_many :bs
end

class B < ActiveRecord::Base
  has_one :c
  belongs_to :a
end

class C < ActiveRecord::Base
  belongs_to :b
end

I want to get json data containing all B's and C's for an A. I tried a number of things similar to:

render json: @as, :include => [:bs => [:include=>[:c]]

but nothing works. What would be a good way to do this.

6 Answers 6

51

Refer to ActiveModel::Serializers::JSON#as_json to see the options you can pass to render :json. To quote:

To include associations use :include ...

Second level and higher order associations work as well:

user.as_json(:include => { :posts => {
                             :include => { :comments => {
                                             :only => :body } },
                             :only => :title } })
# => { "id": 1, "name": "Konata Izumi", "age": 16,
#      "created_at": "2006/08/01", "awesome": true,
#      "posts": [ { "comments": [ { "body": "1st post!" }, { "body": "Second!" } ],
#                   "title": "Welcome to the weblog" },
#                 { "comments": [ {"body": "Don't think too hard" } ],
#                   "title": "So I was thinking" } ]
#    }

It's not necessary to call to_json or as_json directly, as render :json does it automatically.

Sign up to request clarification or add additional context in comments.

4 Comments

If you're stuck in Rails 2, render() doesn't support :include, but to_json() does. In that case, it makes sense to call render :json => @as.to_json(:include => :bs).
to_json sometimes break the nest struct
@Albert.Qing If you're having a specific problem you should post it as a new question.
9

You need to pass in hash instead of array

render :json => @as.to_json(:include => { :bs => {:include =>:c} })

Comments

6

For those looking to do the same thing but with ruby's new syntax instead, it would be something like this

render json: @as.to_json(include: { bs: { include: :c}})

And to extract only some attributes:

render json: @a.to_json(only: [:name, :phone, :phone_mobile], include: { bs: { only: :email, include: {c: {only: [:id, :name]}}}})

Comments

2

Try

render :json => @as.to_json(:include => {:bs => :c})

Comments

2

Try this:

render json: @tiquets, :include => { :enterprise => {:include => { :location => {:only => :lo_name } },:only => :en_name } } }

4 Comments

@OwaisKureshi A code-only answer might not be a good one, but it's still an answer. I would recommend you this post about the LQPRQ: You're doing it wrong: A plea for sanity in the Low Quality Posts queue
@FelixSFD , Well I agree, but there should be some text explaining the answer, or he can just post this as a comment?
@OwaisKureshi It's recommended and preferable to explain the code a bit. But even without explanation, this is an answer.
@FelixSFD Alright I got your point, thanks for explaining, I have retracted my comment.
0

Try this:

render json: @as.to_json(include:{bs: {include:{c:}}})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.