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”
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?
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:
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
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.