[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Harish Dewangan

1/26/2009 11:31:00 AM

Hi All,

i am new ruby on rails n i am currently working on xml with Ruby on
rails.

i prepared string and passed that string into simplexml module
xml_data = "<records>
<record><first>1</first><second>2</second><third>3</third></record>
<arn><child1>First</child1><child2>Second</child2><child3>Third</child3></arn>
<arn><child1>First</child1><child2>Second</child2><child3>Third</child3></arn>
<arn><child1>First</child1><child2>Second</child2><child3>Third</child3></arn>
</records>"
data = XmlSimple.xml_in(xml_data)

i got pure xml now and converting that xml into model object and when i
am trying to save the object it is inserting with the junk values as
shown below

'---- "1"', '--- - "2"', '--- - "3"', '--- - First', '--- - Second',
'--- - third'

is any one can help me plz whats the prb here ..
if you have any solution can you send me plz on my id :
harish.dew@gmail.com

Thanks in Advance
Harish
--
Posted via http://www.ruby-....

4 Answers

Brian Candler

1/26/2009 2:32:00 PM

0

Harish Dewangan wrote:
> i am new ruby on rails

...

> i got pure xml now and converting that xml into model object and when i
> am trying to save the object it is inserting with the junk values as
> shown below

Problems with Rails and/or ActiveRecord would best be asked on a Rails
mailing list (here you have found the general Ruby programming language
list)

From a Ruby point of view, I'd suggest you try using 'inspect' on your
model before saving it. e.g.

# create your model
foo = Foo.new(...)

# show it
STDERR.puts foo.inspect

foo.save!

This divides your problem in half: if @attributes contains the right
data then the problem is when ActiveRecord saves it, but if @attributes
contains the bad data then you can work on the first part of your
program.

> '---- "1"', '--- - "2"', '--- - "3"', '--- - First', '--- - Second',
> '--- - third'

Strange, looks like it's converting to YAML for some reason.
--
Posted via http://www.ruby-....

Harish Dewangan

1/27/2009 2:14:00 PM

0

Brian Candler wrote:
> Harish Dewangan wrote:
>> i am new ruby on rails
>
> ...
>
>> i got pure xml now and converting that xml into model object and when i
>> am trying to save the object it is inserting with the junk values as
>> shown below
>
> Problems with Rails and/or ActiveRecord would best be asked on a Rails
> mailing list (here you have found the general Ruby programming language
> list)
>
> From a Ruby point of view, I'd suggest you try using 'inspect' on your
> model before saving it. e.g.
>
> # create your model
> foo = Foo.new(...)
>
> # show it
> STDERR.puts foo.inspect
>
> foo.save!
>
> This divides your problem in half: if @attributes contains the right
> data then the problem is when ActiveRecord saves it, but if @attributes
> contains the bad data then you can work on the first part of your
> program.
>
>> '---- "1"', '--- - "2"', '--- - "3"', '--- - First', '--- - Second',
>> '--- - third'
>
> Strange, looks like it's converting to YAML for some reason.

Sir,

when i add the Code what you gave like STDERR.puts foo.inspect
it printed like this, i think this is correct data.
#<XMLDemo first: ["1"], second: ["2"], third: ["3"], child1: ["First"],
child2: ["Second"], child3: ["Third"], id: nil>

But still i am facing the same problem. plz help to solve this prb

Thanks in Advance
Harish Kumar Dewangan




--
Posted via http://www.ruby-....

Brian Candler

1/27/2009 2:58:00 PM

0

Harish Dewangan wrote:
> when i add the Code what you gave like STDERR.puts foo.inspect
> it printed like this, i think this is correct data.
> #<XMLDemo first: ["1"], second: ["2"], third: ["3"], child1: ["First"],
> child2: ["Second"], child3: ["Third"], id: nil>

No, that's wrong, and it explains why your data is getting serialized as
YAML.

Each of your attributes is an array:

["1"]

when it should be just a string

"1"

XmlSimple is giving you an array because you might get multiple elements
with the same name:

<record>
<first>foo</first>
<first>bar</first>
<first>baz</first>
...
</record>

So your code which copies from your XML structure into your ActiveRecord
model needs changing, e.g. by using .first or [0] to pick the first
array element.

I have a vague recollection that there's an option you can pass to
XmlSimple for it *not* to put elements inside arrays; look through the
docs.
--
Posted via http://www.ruby-....

Jarmo Pertman

1/30/2009 11:00:00 PM

0

Brian Candler wrote:
> I have a vague recollection that there's an option you can pass to
> XmlSimple for it *not* to put elements inside arrays; look through the
> docs.

You are correct. I just had similar problem as OT.
The option is 'ForceArray' => false

--
Posted via http://www.ruby-....