2017-10-20 57 views
0

我一直在通过一个带有PostgreSQL数据库的Ruby on Rails RSS阅读器。目前,它正确地与现有的供稿同步,但似乎重申了它已经在数据库中拥有的文章。我只想迭代我还没有在数据库中的文章。任何人都可以帮我弄清楚如何用我的sync.rake任务正确地做到这一点,如下图所示?谢谢!如何减少Rails RSS阅读器sync.rake任务中的重复?

namespace :sync do 
    task feeds: [:environment] do 
     Feed.all.each do |feed| 
      content = Feedjira::Feed.fetch_and_parse feed.url 
      content.entries.each do |entry| 
       local_entry = feed.articles.where(title: entry.title).first_or_initialize 
       text = Nokogiri::HTML(open(entry.url)) 
       local_entry.update_attributes(content: text, author: entry.author, url: entry.url, published: entry.published) 
       p "Synced Entry - #{entry.title}" 
      end 
      p "Synced Feed - #{feed.name}" 
     end 
    end 
end 

回答

0

我相信你可以使用published现场找到最新的出版物,存储在数据库中。

所以,你可以运行类似:

last_entry = feed.articles.last 
content.entries.reject { |e| e.published < last_entry.published } 

而在此之后遍历集合过滤创建新条目。