Carter Shaw
Carter Shaw

Reputation: 859

Rails as_json not including all associations

For arguments sake, lets say I have legs as my object. I'm trying to call as_json on it and include all associations - which are set up correctly as it works perfectly in the console.

This is what I have:

@legs.as_json(
  :include => {
    :shoes => {
      :include => {
        :left_shoe => {
          :include => {
            :shoe_lace => {:include => :sock}
          }
        }
      },
      :right_shoe => {
        :include => {
          :shoe_lace => {:include => :sock}
        }
      }
    }
  }
)

For some reason it's not including right_shoe. It just stops at left_shoe. Any ideas?

Upvotes: 0

Views: 99

Answers (2)

Junior Joanis
Junior Joanis

Reputation: 345

Have you ever try this gem, this is pretty cool to manipulate JSON model association:

https://github.com/fabrik42/acts_as_api

Upvotes: 0

rainkinz
rainkinz

Reputation: 10393

I think as per the comments your right shoe is at the wrong level. Try this:

@legs.as_json(
  :include => {
    :shoes => {
      :include => {
        :left_shoe => {
          :include => {
            :shoe_lace => {:include => :sock}
          }
        },
        :right_shoe => {
          :include => {
            :shoe_lace => {:include => :sock}
          }
        }
      }
    }
  }
)

Upvotes: 1

Related Questions