# Why are these items being persisted?

**URL:** <https://www.ruby-forum.com/t/why-are-these-items-being-persisted/114244>\
**Category:** Rails\
**Created:** [September 10, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/why-are-these-items-being-persisted/114244 "2007-09-10T13:22:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [September 10, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/why-are-these-items-being-persisted/114244/1 "2007-09-10T13:22:24Z")

</div>

Hi all,

In this bit of code:

@feeds = Feed.find\_all  
@feeds.each { |feed|  
items = []  
rssdoc = Hpricot.XML(open(feed.url, ‘User-Agent’ =\> USER\_AGENT))  
(rssdoc/:item).each {|item| items.push(rss\_item\_to\_db\_item(item,  
@query.id))}  
feed.items = items  
}

Feed is an Active Record database object, as is Item.

So I’m looping through each Feed object, and getting an array of items  
back.

The Item objects are being persisted in the database when I assign the  
array to the feed.items object.

I didn’t expect that to happen! Could anyone tell me why it is happening  
please?! 🙂

Many Thanks,  
Steven

---

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [September 10, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/why-are-these-items-being-persisted/114244/2 "2007-09-10T20:04:20Z")

</div>

hi steven,

this is what i know,

in your feed model i guess there is a association defined, with  
“items” as name of association,  
so when ever you do some thing like feed.items = {Some Item Objects}  
that will be associated to  
this feed db object(row). (i don’t know whether you can override this  
behaviour)

So, in this case if do this it works fine,  
class Feed \< ActiveRecord::Base  
attr\_accesor :related\_tmp\_items # just name sake

has\_many :items

def initialize(\*args)  
super(\*args)  
@related\_tmp\_items=nil  
end

end

now if i say something like this,

feed.related\_tmp\_items = Item.find() # find what ever i want…

Note:  
One more way is ( i din’t try this), using Transaction::Simple…  
using that you can start your transaction before items are assigned  
and rollback the transaction when you don’t want.

You may face same problem if you try something like this.

feed.item\_ids = {list of item object id’s}… # basically many to many  
relation-ship

P.s: Sorry if i am not answering your problem statement.

On Sep 10, 4:22 pm, Steven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sshingler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@sshingler](https://www.ruby-forum.com/u/sshingler)\
**Post date:** [September 11, 2007, 11:52am UTC](https://www.ruby-forum.com/t/why-are-these-items-being-persisted/114244/3 "2007-09-11T11:52:32Z")

</div>

Many Thanks raghukumar! That worked really nicely. Actually it seems I  
am able to do feed.related\_tmp\_items = items  
Cheers,  
Steven
