如何减少Rails RSS阅读器sync.rake任务中的重复?

时间:2017-10-20 14:12:44

标签: ruby-on-rails ruby postgresql

我一直在使用带有PostgreSQL数据库的Ruby on 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

1 个答案:

答案 0 :(得分:0)

我相信您可以使用published字段来查找存储在数据库中的最新发布。

所以你可以运行类似的东西:

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

然后迭代过滤集合以创建新条目。