Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#4840

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>



Reported by Cedric Fung · June 12th, 2010 @ 10:11 AM

State: resolved
Milestone: 3.0.5
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • 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>
    

    June 13th, 2010 @ 03:16 AM

  2. knoopx (at gmail)
    knoopx (at gmail)

    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'
    

    September 30th, 2010 @ 03:02 PM

  3. knoopx (at gmail)
    knoopx (at gmail)

    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.

    September 30th, 2010 @ 03:08 PM

  4. knoopx (at gmail)
    knoopx (at gmail)

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

    September 30th, 2010 @ 03:14 PM

  5. José Valim
    José Valim
    • 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?

    September 30th, 2010 @ 03:29 PM

  6. knoopx (at gmail)
    knoopx (at gmail)

    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

    October 1st, 2010 @ 08:43 AM

  7. José Valim
    José Valim

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

    October 1st, 2010 @ 09:54 AM

  8. David Trasbo
    David Trasbo
    • State changed from open to incomplete

    Please add tests to the patch.

    October 9th, 2010 @ 04:22 PM

  9. Jeremy Kemper
  10. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 16th, 2010 @ 02:42 AM

  11. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    November 8th, 2010 @ 01:49 AM

  12. Santiago Pastorino
    Santiago Pastorino

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

    November 11th, 2010 @ 03:37 AM

  13. Santiago Pastorino
  14. Cesario
    Cesario
    • Tag set to rails 3.0.0, activecord, serialization, xml

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

    November 18th, 2010 @ 08:05 AM

  15. Cesario
    Cesario

    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

    November 18th, 2010 @ 08:09 AM

  16. Cesario
    Cesario

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

    https://gist.github.com/704770

    November 18th, 2010 @ 08:23 AM

  17. José Valim
    José Valim
    • Assigned user changed from José Valim to Aaron Patterson

    Cesario, was this applied already?

    November 25th, 2010 @ 09:48 AM

  18. Cesario
    Cesario

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

    November 25th, 2010 @ 10:08 AM

  19. Aaron Patterson
    Aaron Patterson
    • State changed from incomplete to resolved

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

    December 15th, 2010 @ 10:02 PM

  20. Repository
    Repository

    (from [fe0db2b80588446b34f5f89bbe73f04b88fb06c9]) to_xml doesn't work in such case: Event.select('title as t').to_xml [#4840 to_xml doesn't work in such case: Event.select('title as t').to_xml 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

    December 15th, 2010 @ 10:04 PM

  21. Santiago Pastorino