Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:17:57+00:00 2026-05-24T06:17:57+00:00

I have two rails active record classes, School and Instructor linked by a has_and_belongs_to_many

  • 0

I have two rails active record classes, School and Instructor linked by a has_and_belongs_to_many relationship.

I need to query my instructors_controller for instructors for a particular school and return an xml format response. Therefore, in the index method I have this code fragment:

school = School.find(params[:school_id])
@instructors = school.instructors

and later:

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @instructors }
  format.json { render :json => @instructors }
end

But it doesn’t work. Look at this intriguing but baffling sequence:

ruby-1.9.2-p180 :023 >   Instructor.all.first
=> #<Instructor id: 1, name: "Mrs. Smith", instructor_type_id: 1, created_at: "2011-07-24 18:19:40", updated_at: "2011-07-24 18:19:40"> 

ruby-1.9.2-p180 :026 > Instructor.all.first.class
 => Instructor(id: integer, name: string, instructor_type_id: integer, created_at: datetime, updated_at: datetime) 

ruby-1.9.2-p180 :024 > School.first.instructors.first
 => #<Instructor id: 1, name: "Mrs. Smith", instructor_type_id: 1, created_at: "2011-07-24 18:19:40", updated_at: "2011-07-24 18:19:40"> 

ruby-1.9.2-p180 :025 > School.first.instructors.first.class
 => Instructor(id: integer, name: string, instructor_type_id: integer, created_at: datetime, updated_at: datetime) 

ruby-1.9.2-p180 :021 > Instructor.all.first.to_xml
 => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<instructor>\n  <created-at type=\"datetime\">2011-07-24T18:19:40Z</created-at>\n  <id type=\"integer\">1</id>\n  <instructor-type-id type=\"integer\">1</instructor-type-id>\n  <name>Mrs. Smith</name>\n  <updated-at type=\"datetime\">2011-07-24T18:19:40Z</updated-at>\n</instructor>\n" 

Now for the punchline:

ruby-1.9.2-p180 :019 > School.first.instructors.first.to_xml

NoMethodError: undefined method `type' for nil:NilClass
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activesupport-3.0.9/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activerecord-3.0.9/lib/active_record/serializers/xml_serializer.rb:230:in `compute_type'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:22:in `initialize'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:75:in `new'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:75:in `block in serializable_attributes'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:74:in `each'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:74:in `map'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:74:in `serializable_attributes'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:116:in `add_attributes_and_methods'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:103:in `block in serialize'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activemodel-3.0.9/lib/active_model/serializers/xml.rb:102:in `serialize'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/activerecord-3.0.9/lib/active_record/serializers/xml_serializer.rb:175:in `to_xml'
    from (irb):19
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.2-p180@blueprint/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'ruby-1.9.2-p180 :020 > Instructor.all.first.to_xml

What’s going on here ?

Edit: Well, switching from xml to json solved the problem, (to_json does not exhibit the same strangeness as to_xml here), although I would still like to know the explanation for the above behavior.

Also, as I am relatively new to Rails, is there a better way to do what I want to do here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:18:00+00:00Added an answer on May 24, 2026 at 6:18 am

    to_xml() tries to define a type attribute for each model attribute. For example, a User model with an age (INT) attribute will have: <age type="integer">42</age>. This type information is not encoded in JSON, which is why to_json() works for you. Add this method to your Instructor model:

    def to_xml(options = {})
      to_xml_opts = {:skip_types => true} # no type information, not such a great idea!
      to_xml_opts.merge!(options.slice(:builder, :skip_instruct))
      # a builder instance is provided when to_xml is called on a collection of instructors,
      # in which case you would not want to have <?xml ...?> added to each item
      to_xml_opts[:root] ||= "instructor"
      self.attributes.to_xml(to_xml_opts)
    end
    

    This would however make your XML devoid of any type information – not good if someone has a JAVA REST client. A better strategy would be to filter self.attributes.slice(*keys).to_xml(to_xml_opts) where keys is an array of model attributes you want in the XML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Active Record outside of rails and I have two AR classes similar
I have two rails Active Record models groups and accounts group has_many :accounts id
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
Suppose I have two classes in a Rails application: class Subject < ActiveRecord::Base def
We have two Rails models: Person and Administrator . We're disallowing removal of Administrator
I have two Rails 3 applications that will share portions of the same database
I am new to rails validation. I have two fields: field :feed_entitlements, :type =>
I have installed two different version of rails in same gem set. When I
I have had this problem w/ two seperate WYSWYG editors in my rails application
I'm working on a Rails application where I have some a set of two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.