Title: Nested serialization with to_json and to_xml with array and
hash element

class Person < ActiveRecord::Base
        has_one :address
        has_one :job

        def office()
                return "Executive_Suite"
        end
end


class Address < ActiveRecord::Base
        belongs_to :person
end


class Job < ActiveRecord::Base
        belongs_to :person
        has_one :paygrade

        def Title
                return "Engineer"
        end
end


class PayGrade < ActiveRecord::Base
        belongs_to: job

end


Given an instance of a Person, I am able to serialize in XML and JSON
in some simpler nested manners such as this:

@person = Person.find_by_id(1)


# render the results with multiple peer associations with no drill
down
respond_to do |format|
      format.json { render.json(:json=>@person.to_json(:include=>
[:address, :job], :methods=>:office) }
      format.xml { render.json(:json=>@person.to_json(:include=>
[:address, :job], :methods=>:office) }
      }
end


# render the results with 1 association requiring deep drill down
respond_to do |format|
      format.json { render.json(:json=>@person.to_json(:include=>
{:job=>{:include=>:paygrade}}) }
      format.xml { render.json(:json=>@person.to_json(:include=>{:job=>
{:include=>:paygrade}}) }
      }
end


Where I continually run into problems is where I have multiple peer
associations at the same level
with one of them needing further drill down into one of its
association.

For instance I thought I could simply do this to serialize the Person
instance, along with their address, job with Paygrade.
Whenever I try to do this:
respond_to do |format|
      format.json { render.json(:json=>@person.to_json(:include=>
[:address, {:job=>{:include=>:paygrade}}] }
      format.xml { render.json(:json=>@person.to_json(:include=>
[:address, {:job=>{:include=>:paygrade}}] }
      }
end


I always get the following error: You have a nil object when you
didn't expect it.  Error evaluating nil.ma.
Based on my understanding the :include is expecting an array but I'm
adding a hash as an element and Rails
is not expecting it in the way I'm specifying?

Where I also continually run into problems is where I have multiple
peer associations at the same level
with one of them needing requiring a custom method to be serialized.



Question 1: What is the correct way to be able to specify that I need
to serialize the Person with address
and job along with the paygrade for the job?  This is the multiple
peers with deep drill down problem mentioned above.

Question 2: What is the correct way to be able to specify that I need
to serialize the Person with address
and job along with the custom Job Title method with the paygrade?
When I try this, I get error:

respond_to do |format|
      format.json { render.json(:json=>@person.to_json(:include=>
[:address, {:job=>{:methods=>:title, :include=>:paygrade}}] }
      format.xml { render.json(:json=>@person.to_json(:include=>
[:address, {:job=>{:methods=>:title, :include=>:paygrade}}] }
      }
end

Please advise, and thanks in advance.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to