0

我有两个模型 source.rb 和 feed_entries.rb。如何让 feed_entries 每 3 分钟下载一次。我试过 gem 'whenever' 并收到错误 NoMethodError: private method `select'

源码.rb

class Source
  include Mongoid::Document
  include Mongoid::Timestamps

  field :url, type: String
  field :title, type: String
  field :site, type: String

  after_save :load_feed_entries

  has_many :feed_entries, dependent: :destroy

  def load_feed_entries
    last_entry = self.feed_entries.last
    if last_entry.nil?
      feed = Feedjira::Feed.fetch_and_parse(self.url)
    else
      feed = Feedjira::Feed.fetch_and_parse(self.url,
        :if_modified_since => last_entry.published_at.to_time)
    end

    Source.add_entries(feed.entries, self.id)
  end

  def self.update_all_feeds(urls)
    Feedjira::Feed.fetch_and_parse(urls,
      :on_success => lambda { |url, feed|
        rss = Source.select("sources.id").where(:url => url).first
        Source.add_entries(feed.entries, rss.id)
      }
    )
  end

  private
    def self.add_entries(entries, stream_id)
      entries.each do |entry|
          unless  FeedEntry.where(guid: entry.id).exists?
            FeedEntry.create!(
              :name          => entry.title,
              :summary       => entry.summary,
              :url           => entry.url,
              :content       => entry.content,
              :published_at  => entry.published,
              :guid          => entry.id,
              :source_id => stream_id,
            )
          end
      end
    end
end

饲料耙

task :collect => :environment do
  urls = Source.select("sources.url").all.map { |v| v[:url] }
  Source.update_all_feeds(urls)
end
4

1 回答 1

0

源码.rb

  def self.update_all_feeds(urls)
    Feedjira::Feed.fetch_and_parse(urls,
      :on_success => lambda { |url, feed|
        rss = Source.only(:id).where(:url => url).first
        Source.add_entries(feed.entries, rss.id)
      }
    )
  end

饲料耙

task :collect => :environment do
  urls = Source.only(:url).all.map { |v| v[:url] }
  Source.update_all_feeds(urls)
end
于 2014-12-12T13:27:54.937 回答