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:

konata.to_json(:include => { :posts => {

:include => { :comments => {

:only => :body } },

:only => :title } })

to_json works if I’m only including one association, but fails with:

“You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.macro”

Here is my code:

This works:
format.json { render :json => @blog_entry_comments.to_json( :include
=> { :blog_entry => { :include => [ :blog, :created_by ] } } ) }

This fails:
format.json { render :json => @blog_entry_comments.to_json( :include
=> [ { :blog_entry => { :include =>
[ :blog, :created_by ] } }, :created_by ] ) }

The function is failing in activerecord -> serialization ->
serialization.rb line 55:

records = case @record.class.reflect_on_association(association).macro

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

On Jan 21, 7:03 am, Calvin Ng [email protected]

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.

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
  ]

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.