Multiple associations with to_json

How does the to_json :include option work with multiple associations
with multiple levels of associations? I'm using edge rails and found
this in the docs:

# 2nd level and higher order associations work as well:

to_xml fails in the same fashion as well. Syntax?

Menator,

Have you found a solution for this problem? I am continually running
into this same error you stated above. It happens whenever I have
multiple associations that exist at the same level (let's call them
siblings) with one of them requiring a hash either to further traverse
down to get deeper associations with a nested :include or when needing
to specify other options on one of the siblings to qualify one of the
:methods, :only or :except options.

Please advise if you have found a solution to this issue?

Hi,

I am facing the same problem and looks like the following construction
works:

booking..to_json(:include => {:booking_state => {}, :customer =>
{include => :customer_phones}})

Not as clean and nice as the expected though:

booking..to_json(:include => [:booking_state, {:customer => {include
=> :customer_phones}}])

So I thought better to share

Cheers

Multiple json inculded associations without parameters
model_name.to_json(
  :include => [
    :assoc_1,
    :assoc_2
  ])

Works

Multiple json inculded associations with parameters
model_name.to_json(
  :include => [
    {:assoc_1 => params},
    {:assoc_2 => params}
  ])

Fails

Multiple json inculded associations mixing parameters
model_name.to_json(
  :include => [
    assoc_1,
    {:assoc_2 => params}
  ])

Fails

Any work around?

Aj Oneal wrote:

Multiple json inculded associations without parameters
model_name.to_json(
  :include => [
    :assoc_1,
    :assoc_2
  ])

Works

Multiple json inculded associations with parameters
model_name.to_json(
  :include => [
    {:assoc_1 => params},
    {:assoc_2 => params}
  ])

Fails

Multiple json inculded associations mixing parameters
model_name.to_json(
  :include => [
    assoc_1,
    {:assoc_2 => params}
  ])

Fails

Any work around?

First time poster... Hope this helps! Sorry it's rather late...
I found that allocating the hashes to variables first did the trick to
me. So you had this:

model_name.to_json(
  :include => [
    {:assoc_1 => params},
    {:assoc_2 => params}
  ])

That becomes this:
assoc_1 = {:assoc_1 => params}
assoc_2 = {:assoc_2 => params}
model_name.to_json(:include => {:assoc_1 => assoc_1, :assoc_2 =>
assoc_2})

Incidentally, you can also tack on your :only argument as well, works a
charm.

So you're telling me that the solution to the problem is this simple?

model_name.to_json(
:include => [
   {:arbitrary_name_1 => {:assoc_1 => params} },
   {:arbitrary_name_2 => {:assoc_2 => params} }
])

Ugh... why isn't that in the documentation!?

I'll have to give it a try sometime.

I finally came back around to playing with this. A very important thing
to note is that you get the same error message whether you do it
completely wrong or if you just pluralize your models wrong.

In this example
A Customer has many cars; A Car belongs to exactly one customer
A Car has many work orders; A Work Order belongs to one car
A Work Order has many purchases; A purchase belongs to one Work Order
A purchase has exactly one part; A part has many purchases
A Work Order has many comments

    receipt = WorkOrder.find(params[:id])
    @receipt = receipt.to_json(
      :include => {
        :car => {
          :include => :customer
        },
        :purchases => {
          :include => :part
        },
        :comments => {}
      }
    )

I had to troubleshoot for over an hour until I finally got it right. If
I had been careful with the pluralization I would have gotten there a
lot sooner.

    # All of these simpler configurations work as well
      :include => :purchases

      :include => {
        :purchases => {
          :include => :part
        }
      }

      :include => {
        :purchases => {
          :include => {
            :part => {
              :only => :part
            }
          }
        }
      }

      :include => {
        :purchases => {
          :include => {
            :part => {
              :only => :part
            }
          },
          :only => :price
        }
      }

      # array
      :include => [
        :purchases,
        :car
      ]