This project is archived and is in readonly mode.

#4840 ✓resolved
Cedric Fung

to_xml doesn't work in such case: Event.select('title as t').to_xml

Reported by Cedric Fung | June 12th, 2010 @ 10:11 AM | in 3.0.5

I use ruby 1.9.2 preview3 and rails 3b4

The simple select works well

irb(main):042:0> Event.select('title').limit(3).to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<events type=\"array\">\n  <event>\n    <title>&#9733;&#31908;&#34880;PARTY&#9733;&#31532;&#19968;&#20987;&#65281;</title>\n  </event>\n  <event>\n    <title> &#33655;&#20848;&#29237;&#22763;&#20843;&#20154;&#32452;&#21512;Electric Barbarian</title>\n  </event>\n  <event>\n    <title>&#27861;&#22269;&#20048;&#38431;1984&#20013;&#22269;&#24033;&#28436;@&#28145;&#22323;&#19968;&#28193;&#22530;</title>\n  </event>\n</events>\n"

but when I try this

irb(main):043:0> Event.select('title as t').limit(3).to_xml
NoMethodError: undefined method type' for nil:NilClass

from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0.beta4/lib/active_record/serializers/xml_serializer.rb:230:in `compute_type'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:20:in `initialize'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:73:in `new'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:73:in `block in serializable_attributes'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:72:in `each'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:72:in `map'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:72:in `serializable_attributes'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:114:in `add_attributes_and_methods'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:101:in `block in serialize'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.beta4/lib/active_model/serializers/xml.rb:100:in `serialize'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0.beta4/lib/active_record/serializers/xml_serializer.rb:175:in `to_xml'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib/active_support/xml_mini.rb:107:in `to_tag'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/array/conversions.rb:159:in `block (2 levels) in to_xml'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/array/conversions.rb:159:in `each'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/array/conversions.rb:159:in `block in to_xml'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/array/conversions.rb:158:in `to_xml'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0.beta4/lib/active_record/relation.rb:331:in `method_missing'
from (irb):43
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.0.beta4/lib/rails/commands/console.rb:47:in `start'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.0.beta4/lib/rails/commands/console.rb:8:in `start'
from /opt/ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.0.beta4/lib/rails/commands.rb:23:in `&lt;top (required)&gt;'
from script/rails:9:in `require'
from script/rails:9:in `&lt;main&gt;'irb(main):044:0&gt; </code>



Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh June 13th, 2010 @ 03:16 AM

    • Tag changed from select, to_xml to ruby19, select, to_xml

    Just to add to the ticket it is working fine with ruby 1.8.7. I am getting following output

    <?xml version="1.0" encoding="UTF-8"?>
    <events type="array">
      <event>
        <t type="NilClass">railsconf</t>
      </event>
    </events>
    
  • knoopx (at gmail)

    knoopx (at gmail) September 30th, 2010 @ 03:02 PM

    I'm having similar problems when including associations.

      2) Api::V1::StreamablesController should show details for a movie
         Failure/Error: get(:show, :id => @movie.id, :format => :xml)
         undefined method `type' for nil:NilClass
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:230:in `compute_type'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:21:in `initialize'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:74:in `new'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:74:in `block in serializable_attributes'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:73:in `each'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:73:in `map'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:73:in `serializable_attributes'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:115:in `add_attributes_and_methods'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:102:in `block in serialize'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:101:in `serialize'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:175:in `to_xml'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:218:in `block (2 levels) in add_associations'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:210:in `each'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:210:in `block in add_associations'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:209:in `add_associations'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:192:in `block in add_includes'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serialization.rb:51:in `block in serializable_add_includes'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serialization.rb:40:in `each'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serialization.rb:40:in `serializable_add_includes'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:191:in `add_includes'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:186:in `add_extra_behavior'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:103:in `block in serialize'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0/lib/active_model/serializers/xml.rb:101:in `serialize'
         # /Users/knoopx/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.0/lib/active_record/serializers/xml_serializer.rb:175:in `to_xml'
         # ./app/controllers/api/v1/streamables_controller.rb:16:in `block (2 levels) in show'
    
  • knoopx (at gmail)

    knoopx (at gmail) September 30th, 2010 @ 03:08 PM

    It's obvious that the problem is caused by this line:

        type = @serializable.class.serialized_attributes.has_key?(name) ?
          super : @serializable.class.columns_hash[name].type
    

    If somehow, the resulting SQL is using a dynamic column, @serializable.class.columns_hash[name].type will fail.
    In my case I'm getting the exception when evaling @serializable.class.columns_hash["the_parent_id"].type which is not present in the schema due it's a dynamic column.

    This exception is thrown even if :skip_types is set to true.

  • knoopx (at gmail)

    knoopx (at gmail) September 30th, 2010 @ 03:14 PM

    If it works on 1.8 and not in 1.9 it may be due is inheriting attributes that should not be inherited.

  • José Valim

    José Valim September 30th, 2010 @ 03:29 PM

    • State changed from “new” to “open”
    • Milestone cleared.
    • Assigned user set to “José Valim”
    • Importance changed from “” to “Low”

    knoopx, could you please provide a patch with tests fixing this error?

  • knoopx (at gmail)

    knoopx (at gmail) October 1st, 2010 @ 08:43 AM

    This patch makes it behave in ruby-1.9 like using ruby-1.8. I've debugged the attributes on both versions and are exactly the same. when

  • José Valim

    José Valim October 1st, 2010 @ 09:54 AM

    The patch needs tests to ensure we won't have regressions.

  • David Trasbo

    David Trasbo October 9th, 2010 @ 04:22 PM

    • State changed from “open” to “incomplete”

    Please add tests to the patch.

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
  • Ryan Bigg

    Ryan Bigg October 16th, 2010 @ 02:42 AM

    • Tag cleared.

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg November 8th, 2010 @ 01:49 AM

    Automatic cleanup of spam.

  • Santiago Pastorino

    Santiago Pastorino November 11th, 2010 @ 03:37 AM

    knoopx are you able to provide a test? can somebody provide a test case for this?.
    Thanks.

  • Santiago Pastorino
  • Cesario

    Cesario November 18th, 2010 @ 08:05 AM

    • Tag set to rails 3.0.0, activecord, serialization, xml

    Here's a patch that includes knoopx's one, and the required test.

  • Cesario

    Cesario November 18th, 2010 @ 08:09 AM

    I just can't attach any file, that's probably why I kept triggering pull requests instead of discussing the issues here last time... here's my branch on GH https://github.com/cesario/rails/commits/lighthouse-4840

  • Cesario

    Cesario November 18th, 2010 @ 08:23 AM

    And here's a gist if you wanna have the patch directly

    https://gist.github.com/704770

  • José Valim

    José Valim November 25th, 2010 @ 09:48 AM

    • Assigned user changed from “José Valim” to “Aaron Patterson”

    Cesario, was this applied already?

  • Cesario

    Cesario November 25th, 2010 @ 10:08 AM

    Nop, there's a pull request opened as I wanted to clean up a bit my repo https://github.com/rails/rails/pull/115

  • Aaron Patterson

    Aaron Patterson December 15th, 2010 @ 10:02 PM

    • State changed from “incomplete” to “resolved”

    @Cesario I've merged in your changes. Thanks!

  • Repository

    Repository December 15th, 2010 @ 10:04 PM

    (from [fe0db2b80588446b34f5f89bbe73f04b88fb06c9]) to_xml doesn't work in such case: Event.select('title as t').to_xml [#4840 state:resolved]

    NilClass.type is no longer defined in Ruby 1.9 and causes ActiveRecord::Base.to_xml to fail with message: undefined method type' for nil:NilClass<br/> https://github.com/rails/rails/commit/fe0db2b80588446b34f5f89bbe73f04b88fb06c9

  • Santiago Pastorino

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages