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.

ActiveRecord and to_xml function error

#6043

LOCAL GEMS

abstract (1.0.0)
actionmailer (3.0.3)
actionpack (3.0.3)
activemodel (3.0.3)
activerecord (3.0.3)
activeresource (3.0.3)
activesupport (3.0.3)

ruby 1.9.2dev (2010-07-11 revision 28618) [x86_64-linux]

see below operate

[zheng.cuizh@localhost linux]$ irb irb(main):001:0> require 'active_record'
=> true

Array.to_xml is not correct.

irb(main):003:0> [{:id=>-1, :name=>"env1", :path=>"/home/zheng.cuizh/demo/demo/portal/../data/ENV/env1", :ctime=>"2010-11-10 19:22:45 +0800", :mtime=>"2010-11-10 19:22:45 +0800"}, {:id=>-1, :name=>"env2", :path=>"/home/zheng.cuizh/demo/demo/portal/../data/ENV/env2", :ctime=>"2010-11-10 19:22:45 +0800", :mtime=>"2010-11-10 19:22:45 +0800"}, {:id=>-1, :name=>"env3", :path=>"/home/zheng.cuizh/demo/demo/portal/../data/ENV/env3", :ctime=>"2010-11-10 19:22:45 +0800", :mtime=>"2010-11-10 19:22:45 +0800"}].to_xml
NoMethodError: undefined method `to_xml' for #<Array:0x000000146d59e8>
        from (irb):3
        from /usr/local/ruby-1.9.2/bin/irb:12:in `<main>'

next code ActiveRecord::Base.new will execute with error obviously,

but it must be execute before use to_xml()

irb(main):006:0> ActiveRecord::Base.new

ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
        from /usr/local/ruby-1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in `retrieve_connection'
        from /usr/local/ruby-1.9.2/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in `retrieve_connection'

irb(main):007:0> 
irb(main):008:0* [{:id=>-1, :name=>"env1", :path=>"/home/zheng.cuizh/demo/demo/portal/../data/ENV/env1", :ctime=>"2010-11-10 19:22:45 +0800", :mtime=>"2010-11-10 19:22:45 +0800"}, {:id=>-1, :name=>"env2", :path=>"/home/zheng.cuizh/demo/demo/portal/../data/ENV/env2", :ctime=>"2010-11-10 19:22:45 +0800", :mtime=>"2010-11-10 19:22:45 +0800"}, {:id=>-1, :name=>"env3", :path=>"/home/zheng.cuizh/demo/demo/portal/../data/ENV/env3", :ctime=>"2010-11-10 19:22:45 +0800", :mtime=>"2010-11-10 19:22:45 +0800"}].to_xml
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<objects type=\"array\">\n  <object>\n    <id type=\"integer\">-1</id>\n    <name>env1</name>\n    <path>/home/zheng.cuizh/demo/demo/portal/../data/ENV/env1</path>\n    <ctime>2010-11-10 19:22:45 +0800</ctime>\n    <mtime>2010-11-10 19:22:45 +0800</mtime>\n  </object>\n  <object>\n    <id type=\"integer\">-1</id>\n    <name>env2</name>\n    <path>/home/zheng.cuizh/demo/demo/portal/../data/ENV/env2</path>\n    <ctime>2010-11-10 19:22:45 +0800</ctime>\n    <mtime>2010-11-10 19:22:45 +0800</mtime>\n  </object>\n  <object>\n    <id type=\"integer\">-1</id>\n    <name>env3</name>\n    <path>/home/zheng.cuizh/demo/demo/portal/../data/ENV/env3</path>\n    <ctime>2010-11-10 19:22:45 +0800</ctime>\n    <mtime>2010-11-10 19:22:45 +0800</mtime>\n  </object>\n</objects>\n"

now to_xml is work well

maybe some functions or status need initialize in ActiveRecord::Base.new,
but some time I just need use to_xml or to_json not ActiveRecord::Base.
please give me some advise.

Reported by Charles · November 23rd, 2010 @ 03:59 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    Can you show gem list builder?

    November 24th, 2010 @ 09:26 PM

  2. Charles
    Charles

    [zheng.cuizh@localhost source]$ gem list builder

    LOCAL GEMS

    builder (2.1.2)

    November 25th, 2010 @ 05:11 AM

  3. Cesario
    Cesario

    Hi Charles,

    Can you try to use require 'active_support/core_ext/hash/conversions' instead?

    November 26th, 2010 @ 08:22 AM

  4. Aditya Sanghi
    Aditya Sanghi
    • State changed from new to invalid

    i confirm that @cesario's suggestion above work. @charles, you need to require the correct libraries to ensure that to_xml works on an array (the answer is to require 'active_support/core_ext/hash/conversions', not 'active_record' unless of course you need active_record for other purposes).

    Marking ticket as invalid. Please comment if you disagree or want further conversation on this.

    November 27th, 2010 @ 10:18 AM

  5. Aditya Sanghi
  6. Charles
    Charles

    Thanks everyone and your suggest.

    November 29th, 2010 @ 08:48 AM