# Saving a record

**URL:** <https://www.ruby-forum.com/t/saving-a-record/103175>\
**Category:** Rails\
**Created:** [May 2, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175 "2007-05-02T18:55:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 2, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175/1 "2007-05-02T18:55:51Z")

</div>

As I plod on in my journey to grasp Rails (and Ruby) I’m often  
surprised by the difficulty in figuring out how to do something. Most  
of the time the surprise is a pleasant one, in that it’s much easier  
than I expected. But then sometimes something that I expect to be  
easy leaves me scratching my head.

Today I am trying to retrieve XML data from a remote server, parse  
that data into a class, then save it to the database.

I “knew” the XML part would be hard and the saving easy.

Well, retrieving and parsing the data took maybe five minutes to get  
working, instead of all day like I expected 🙂  
But now I can’t properly save it.

The columns always end up filled with nulls. To simply the problem I  
eliminated the retrieval part and tried to just create and save, like  
this:

#in the account\_application controller

account\_application = AccountApplication.new  
account\_application.first\_name = “George”  
account\_application.save

A row gets created, but first\_name is null.  
In the model file I have

class AccountApplication \< ActiveRecord::Base

attr\_writer :first\_name

Surely I’m doing something that’s just plain wrong, but I am stumped.

Thanks,  
-George

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 2, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175/2 "2007-05-02T19:04:52Z")

</div>

> attr\_writer :first\_name

Get rid of this attr\_writer line and any others that match the columns  
in  
your account\_applications table…

-philip

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 2, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175/3 "2007-05-02T19:32:56Z")

</div>

On May 2, 2007, at 11:16 AM, Philip H. wrote:

> Get rid of this attr\_writer line and any others that match the  
> columns in  
> your account\_applications table…

Thanks. I guess those are already done for me. That changes my  
question 🙂  
I’d put that in trying to solve a different problem in get the data  
out of the REXML document. In my rhtml file I have

\<% @doc.elements.each(“application\_list/application”) { |element| %\>  
\<%= element.elements[“first\_name”] %\>  
\<% } %\>

I get what I expect, the applicant’s first name. But if (back in the  
controller) I try to assign it:

account\_application.first\_name = element.elements[“first\_name”]

I get a first\_name of — &id001 !ruby/object:REXML::Element

So unlike I thought, I now see I don’t grasp how to extract the data  
from the document.

I appreciate the list’s patience while I try to sort out what must be  
some pretty fundamental things.  
-George

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 2, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175/4 "2007-05-02T20:09:00Z")

</div>

> account\_application.first\_name = element.elements[“first\_name”]
> 
> I get a first\_name of — &id001 !ruby/object:REXML::Element
> 
> So unlike I thought, I now see I don’t grasp how to extract the data  
> from the document.

I haven’t used REXML, but that string looks like the string version of a  
ruby object… so you haven’t gone far enough… you’ll need something  
like:

element.elements[“first\_name”].value

or something to retrieve the actual value of that element, not the  
object  
itself. CHeck REXML’s docs for what “value” should actually be.

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 2, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175/5 "2007-05-02T21:10:08Z")

</div>

> With that in the .rhtml file I get a list of the first names. So I
> 
> This is a bit frustrating. I know exactly what I want to do, but I  
> just don’t know how to explain it to Ruby. 😉

What about trying “.to\_s” ? I think the \<%= perhaps does the implicitly  
perhaps…

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 2, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175/6 "2007-05-02T22:14:14Z")

</div>

On May 2, 2007, at 1:18 PM, Philip H. wrote:

> What about trying “.to\_s” ? I think the \<%= perhaps does the  
> implicitly  
> perhaps…

Well, that explained a few things. INstead of just the name that gives  
\<first\_name\>George\</first\_name\>

And viewing the source of the web page produced by the rhtml file  
reveals that that is what’s in there. I hadn’t realized it, of  
course, because the “unknown tags” get ignored, and so I saw only the  
name.  
It seems like I’m still one level too high, but I can’t see any way  
to go any deeper. I’ve tried a dozen different things.  
Is it possible that the XML I’m receiving is malformed, and REXML  
is considering that whole thing to be the data?

I guess I’m biting off too much at once, having done little before  
with XML, Ruby, or Rails.

It sure seems like I’m close, though, and I’ve managed to already get  
so much working in Rails that I thought I could iron out all the  
small wrinkles.

Thanks for your help, Phillip. I appreciate it. I’ve got some more  
digging to do.

-George

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 2, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/saving-a-record/103175/7 "2007-05-02T20:37:04Z")

</div>

On May 2, 2007, at 12:20 PM, Philip H. wrote:

> I haven’t used REXML, but that string looks like the string version  
> of a  
> ruby object… so you haven’t gone far enough… you’ll need something  
> like:
> 
> element.elements[“first\_name”].value
> 
> or something to retrieve the actual value of that element, not the  
> object  
> itself. CHeck REXML’s docs for what “value” should actually be.

Thanks Phillip.  
Yes, I figured I was in the wrong “place” in the tree, but then I  
don’t understand what magic allows this to work:

\<% @doc.elements.each(“application\_list/application”) { |element| %\>  
\<%= element.elements[“first\_name”] %\>  
  
\<% } %\>

With that in the .rhtml file I get a list of the first names. So I  
don’t get how that code knows how to extract the text, but when I try  
to assign from it I get the object string instead.

I’ve tried several things, obviously without success. For instance,  
using get\_text:

account\_application.first\_name = app\_data.elements  
[“first\_name”].get\_text

yields: — !ruby/object:REXML::Text

This is a bit frustrating. I know exactly what I want to do, but I  
just don’t know how to explain it to Ruby. 😉

-George

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 3, 2007, 4:46am UTC](https://www.ruby-forum.com/t/saving-a-record/103175/8 "2007-05-03T04:46:44Z")

</div>

> just don’t know how to explain it to Ruby. 😉  
> Success.  
> For completeness, here’s the working version:

@doc.elements.each(“application\_list/application”) { |app\_data|  
account\_application = AccountApplication.new  
account\_application.first\_name = app\_data.elements  
[“first\_name”].text #.text was the missing ingredient  
…  
account\_application.save  
}

Seems obvious now.  
I’m sure that I had already tried text, but I guess something else  
had been wrong and I made two changes at once.

(Phillip , thanks for your help)

-George
