Why are these items being persisted?

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,
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

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?! :slight_smile:

Many Thanks,

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

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)


now if i say something like this,

feed.related_tmp_items = Item.find() # find what ever i want…

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

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

On Sep 10, 4:22 pm, Steven S. [email protected]

Many Thanks raghukumar! That worked really nicely. Actually it seems I
am able to do feed.related_tmp_items = items

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs