# Store data in db from XML

**URL:** <https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353>\
**Category:** Rails\
**Created:** [September 29, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353 "2009-09-29T12:57:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smit\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@smit\_shah](https://www.ruby-forum.com/u/smit_shah)\
**Post date:** [September 29, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353/1 "2009-09-29T12:57:00Z")

</div>

Hi All…

I am trying to parsing data from XML. But, I am not able to store it in  
database.  
So, any one can help me out???

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![smit\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@smit\_shah](https://www.ruby-forum.com/u/smit_shah)\
**Post date:** [September 29, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353/2 "2009-09-29T14:38:50Z")

</div>

2009/9/29 Smit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi All…
> 
> I am trying to parsing data from XML. But, I am not able to store it in  
> database.  
> So, any one can help me out???

What is the problem that is preventing you from storing it in the db?

Colin

---

<div class="post-metadata">

**Author:** ![smit\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@smit\_shah](https://www.ruby-forum.com/u/smit_shah)\
**Post date:** [September 30, 2009, 6:05am UTC](https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353/3 "2009-09-30T06:05:37Z")

</div>

Colin L. wrote:

> 2009/9/29 Smit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Hi All…
> > 
> > I am trying to parsing data from XML. But, I am not able to store it in  
> > database.  
> > So, any one can help me out???
> 
> What is the problem that is preventing you from storing it in the db?
> 
> Colin

Hi Colin

I am using feednormalizer gem. I have written code like

@rss = FeedNormalizer::FeedNormalizer.parse open(feed\_url)

I am accessing the values like “@rss.entries”.  
I have a model named as Data in which I am storing that data.

@data = Data.new  
@data.title = @rss.entries.title

But when I try to store in db it shows me like " allocator undefined for  
Data".  
Any idea???

---

<div class="post-metadata">

**Author:** ![smit\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@smit\_shah](https://www.ruby-forum.com/u/smit_shah)\
**Post date:** [September 30, 2009, 9:57am UTC](https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353/4 "2009-09-30T09:57:33Z")

</div>

2009/9/30 Smit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > What is the problem that is preventing you from storing it in the db?  
> > I have a model named as Data in which I am storing that data.
> 
> @data = Data.new  
> @data.title = @rss.entries.title
> 
> But when I try to store in db it shows me like " allocator undefined for  
> Data".  
> Any idea???

Data is a reserved word in rails (see  
[http://wiki.rubyonrails.org/rails/pages/ReservedWords](http://wiki.rubyonrails.org/rails/pages/ReservedWords)). I suggest  
using a different name for the model.

Colin

---

<div class="post-metadata">

**Author:** ![smit\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@smit\_shah](https://www.ruby-forum.com/u/smit_shah)\
**Post date:** [September 30, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353/5 "2009-09-30T12:29:05Z")

</div>

Colin L. wrote:

> 2009/9/30 Smit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > > What is the problem that is preventing you from storing it in the db?  
> > > I have a model named as Data in which I am storing that data.
> > 
> > @data = Data.new  
> > @data.title = @rss.entries.title
> > 
> > But when I try to store in db it shows me like " allocator undefined for  
> > Data".  
> > Any idea???
> 
> Data is a reserved word in rails (see  
> [http://wiki.rubyonrails.org/rails/pages/ReservedWords](http://wiki.rubyonrails.org/rails/pages/ReservedWords)). I suggest  
> using a different name for the model.
> 
> Colin

Hey thanks dude… Its work  
But now I am suffering from another problem. It stores in db but only  
the first entry of XML file. I have written that code in for loop. My  
code is as follow:

@rss = FeedNormalizer::FeedNormalizer.parse open(feed\_url)  
@rss = @rss.entries.paginate :page =\> params[:page], :per\_page =\> 5 if  
!@rss.blank?  
@abc = Abc.new  
for rss in @rss.entries  
if !@rss.entries.blank?  
@abc.title = rss.title  
@abc.url = rss.url  
end  
end  
@abc.save if !@rss.blank?

---

<div class="post-metadata">

**Author:** ![smit\_shah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@smit\_shah](https://www.ruby-forum.com/u/smit_shah)\
**Post date:** [September 30, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/store-data-in-db-from-xml/176353/6 "2009-09-30T12:45:34Z")

</div>

2009/9/30 Preksha P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â @abc.title = rss.title  
> Â Â @abc.url = rss.url  
> Â end  
> end  
> @abc.save if !@rss.blank?

Would it be better to have the new and the save inside the loop so  
that you make a new one and save it for each item? At the moment you  
make a new object, loop round rewriting the values in @abc each time,  
then save it once.

I presume you realise that the paginate call mean that will only get 5  
at a time?

Also it might be worthwhile studying the rails guide on Debugging Rails  
Apps at  
[http://guides.rubyonrails.org/](http://guides.rubyonrails.org/) The techniques there may help you to  
identify this sort of problem yourself.

Colin
