Forum: Ruby on Rails Need to generate xml to be stored in a database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fe45c81983cee7257fd282baa8132ff3?d=identicon&s=25 casey.ellett (Guest)
on 2008-11-03 23:43
(Received via mailing list)
Forgive my ignorance, but I have searched for a while and cannot find
anything that would help me move in the right direction.
I need to generate xml that will then be recored in the database so
that I can pull out at a later date.
Every bit of info I could find shows how to create the xml and display
it through a .rxml view.

I also have to be able to assemble the data points for the xml from
several different tables (I basically need to pull all of the has_one
and has_many relationships for a particular table and construct the
xml with them).

A push in the right direction would be awesome.

Thanks.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-03 23:45
(Received via mailing list)
On Nov 3, 9:51 pm, "casey.ellett" <casey.ell...@gmail.com> wrote:
> Forgive my ignorance, but I have searched for a while and cannot find
> anything that would help me move in the right direction.
> I need to generate xml that will then be recored in the database so
> that I can pull out at a later date.
> Every bit of info I could find shows how to create the xml and display
> it through a .rxml view.
Check out the builder library - it's what rxml templates use.

Fred
Fe45c81983cee7257fd282baa8132ff3?d=identicon&s=25 Casey Ellett (Guest)
on 2008-11-04 14:55
(Received via mailing list)
I have successfully been able to create the xml I needed as a view, but
how
do I go from that to storing it as a record in the database?


On Mon, Nov 3, 2008 at 5:44 PM, Frederick Cheung
<frederick.cheung@gmail.com
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-04 15:20
(Received via mailing list)
On 4 Nov 2008, at 13:54, Casey Ellett wrote:

> I have successfully been able to create the xml I needed as a view,
> but how do I go from that to storing it as a record in the database?
>
Well, having generated the xml, what's stopping you assigning it to
some attribute of some ActiveRecord object ?

Fred
Fe45c81983cee7257fd282baa8132ff3?d=identicon&s=25 Casey Ellett (Guest)
on 2008-11-04 16:16
(Received via mailing list)
My complete lack of knowledge of how I would do that.  I am very new to
this.
Can I handle that in the method I've put in the controller to collect
the
data and render the xml?

Currently I am just pulling in a specific category object in the
controller
and specifying a view:

def cat_xml
    @category = Category.find(params[:id])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :action => "cat.xml.builder", :layout =>
false }
    end

end

And I have the xml Builder template in:

cat_xml.xml.builder

Where would I assign this view to an ActiveRecord object?

Or am I going down the wrong path here?  I don't actually need to view
the
xml at any point - I'm actually going to pull it out of the database at
a
later point and send it up to a web service. Or I guess I could send it
as
it's generated, I just want to save it to pull it later if need be.

Thanks for taking an interest in helping me out, by the way.

On Tue, Nov 4, 2008 at 9:19 AM, Frederick Cheung
<frederick.cheung@gmail.com
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-04 16:43
(Received via mailing list)
On 4 Nov 2008, at 15:04, Casey Ellett wrote:

>
> cat_xml.xml.builder
>
> Where would I assign this view to an ActiveRecord object?
>
> Or am I going down the wrong path here?  I don't actually need to
> view the xml at any point - I'm actually going to pull it out of the
> database at a later point and send it up to a web service. Or I
> guess I could send it as it's generated, I just want to save it to
> pull it later if need be.
>
If you don't have a view, you don't need a template. you can do
something like

xml = Builder::XmlMarkup.new(:indent => 2)
xml.instruct!
... #buildery stuff here

SomeObject.create :xml_text => xml.target!

Fred
E29b3b72e0a6f7aa6c8f1079a62ce820?d=identicon&s=25 Sheryl Barrow (podo)
on 2008-11-04 18:03
(Received via mailing list)
On Tue, Nov 4, 2008 at 11:09 AM, Frederick Cheung <
frederick.cheung@gmail.com> wrote:

>
>
> > Or am I going down the wrong path here?  I don't actually need to
> > view the xml at any point - I'm actually going to pull it out of the
> > database at a later point and send it up to a web service. Or I
> > guess I could send it as it's generated, I just want to save it to
> > pull it later if need be.
>

We do this very same thing.  It may or may not be the best solution, but
we
store the xml in a temp file on the server, and then have the path
listed in
our MySQL database as text. We just read the whole thing and pass to the
web
service when we're ready to move on.

Our process goes like this:

1) Customer creates a cart and checks out.
2) Call web services to generate the completed order xml.
3) Save order xml into temp text file and put the path into the db.
4) Display confirmation page with totals/taxes (generated from web
service).
5) If confirmed, read order xml and call save method in web service.

It's simple and doesn't require a whole bunch of coding.  It also gives
you
a nice history to look at if there are any problems with the xml.

We remove the xml temp file once the order has been completed so it
doesn't
eat all of our server space.

Hope this helps!
Fe45c81983cee7257fd282baa8132ff3?d=identicon&s=25 Casey Ellett (Guest)
on 2008-11-04 20:31
(Received via mailing list)
I'll give this a go tonight.  Thanks.
Fe45c81983cee7257fd282baa8132ff3?d=identicon&s=25 Casey Ellett (Guest)
on 2008-11-05 05:55
(Received via mailing list)
I got this sorted out.

I ended up creating the new ActiveRecord object and passing the xml from
builder into that.

Thank you for your help.
This topic is locked and can not be replied to.