51224bdd17878b3b19e8987e9bb336a2

Its very simple method which uses FeedTools gem to collect feed items from url.Then I am storing them in a feeddetails table before that (line no 3) I just make sure that there won't be duplicate entry.
I am new to rails I think this method can be refactor.
Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def save_feed  
  @feed = FeedTools::Feed.open('http://losangeles.craigslist.org/apa/index.rss')
     @feed.items.each_with_index do |item,i| 
        feedd = Feeddetail.find(:first,:conditions => ["link =?",item.link]) 
      if feedd.nil? then
          fdetail = Feeddetail.new
          fdetail.feed_id = 1
          fdetail.title = item.title
          fdetail.link = item.link.strip
          fdetail.description = item.description
          fdetail.pubdate=item.published
          fdetail.save
      end
    end   
end

Refactorings

No refactoring yet !

517ffe2436880cf4bddd062d01f06278

Roderic Green

May 12, 2008, May 12, 2008 10:51, permalink

2 ratings. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
def save_feed  
     @feed = FeedTools::Feed.open('http://losangeles.craigslist.org/apa/index.rss')
     @feed.items.each_with_index do |item,i| 
        Feeddetail.find_or_create_by_link(
          :link => item.link.strip,
          :feed_id = i,
          :title => item.title,
          :description = item.description,
          :pubdate=item.published)
    end   
end
51224bdd17878b3b19e8987e9bb336a2

DG

May 12, 2008, May 12, 2008 13:01, permalink

No rating. Login to rate!

Thanks Roderic Green for your reply. :)
Now method is much cleaner & readable.

0333c8c993f63d263c9bc59ad2c35a9b

chalkers

May 22, 2008, May 22, 2008 18:41, permalink

No rating. Login to rate!

For give my ignorance but what's the difference from => and = in the find_or_create_by_link method?

Your refactoring





Format Copy from initial code

or Cancel