7

My setup: Rails 2.3.10, Ruby 1.8.7

I have a rather complicated set of relationships between several models.

class A
  has_many :classB
  has_many :classD
end

class B
  belongs_to :classA
  has_many :classC
end

class C
  belongs_to :classB
  belongs_to :classE
end

class D
  belongs_to :classA
  belongs_to :classE
end

class E
  has_many :classD
  has_many :classC
end

I'm having an issue with the JSON syntax to get all the related information starting with classA. Here's what I have working so far.

classA.to_json(:include => {:classB => {:include => [:classC, :classE]}})

I can't get the syntax working to also include classD and related classE records.

UPDATE Actually something like this might work except that I can't mix hashes and arrays

classA.to_json(:include => [ :classB => { :include => { :classC => { :include => :classE } } },
                             :classD, :classE  ])

Note that I didn't use singular/plural in my example code above but in my real code, I am. Any insights will be much appreciated.

Thanks, Bob

1
  • I assume classA is an object of class A? Commented Dec 23, 2013 at 15:26

3 Answers 3

13

This should work:

classA.to_json(:include => {
  :classB => {:include => {:classC => {:include => :classE}}},
  :classD => {},
  :classE => {},
})
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, that worked, I came to the same conclusion myself, tricky little syntax bugger.
Have an up vote - adding => {} seems to be required when you have multiple relationships at a level, and at least one of which has a sub-include on it
1

Try this, you should only need one :include =>:

classA.to_json(:include => {:classB => [:classC, { :classE => :classD }] })

Comments

1

I don't know if you want classE included through both class C and classD but this should work:

classA.to_json(:include => { :classB => { :include => { :classC => { :include => :classE } } },
                             :classD => { :include => :classE } })

EDIT:

class A
  has_many :bs
  has_many :ds
end

class B
  belongs_to :a
  has_many :cs
end

class C
  belongs_to :b
  belongs_to :e
end

class D
  belongs_to :a
  belongs_to :e
end

class E
  has_many :ds
  has_many :cs
end

@class_a = A.first
@class_a.to_json(:include => { :bs => { :include => { :cs => { :include => :e } } },
                               :ds => { :include => :e } })

6 Comments

I have tried this before and it throws this nondescript error "undefined method `macro' for nil:NilClass"
Okay, then just to make sure, I assume that classA is not actually the model but an instance of classA, right? And after that, I'd check on your associations cause in your example you mix belongs_to and has_many, but the association is just named classA for example. Just make sure you use matching singular / plural syntax in your associations and to_json.
You are right, classA in the JSON line is an instance and I'm not using the proper singular / plural syntax above but in my code, I am. The :include code should all be plurals.
Except for classE which is only associated through belongs_to :classE Sorry, I can't say for sure unless you post your actual code. I have tried my syntax with a similar setup in my own project and it works for me.
Not sure what you mean by "classE which is only associated through belongs_to :classE"? Based on your example, I have updated my question with something that might work but not quite.
|

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.