Question

Basically I have a blog type application that can have posts created from an rss source feed, for which I'm using the feedzirra gem:

def self.update_from_feed

    feed_url = ENV['FEED_URL']
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    add_entries(feed.entries)

end

Thus, one of the methods in my post model handles creating a new post from the parsed feed, and looks something like this:

def self.add_entries(entries)
    entries.each do |entry|
      unless exists? :guid => entry.id
      create!(guid: entry.id, name: entry.title, author: entry.author, summary: entry.summary, published_at: entry.published)
    end
  end

Feedzirra also has an update method available (I've attempted to implement but haven't been able to get it working) in which it can check a feed for changes, and if changes are present it will download the changes in the feed and create new posts as necessary. As you can see in my above method, using unless exists? if a post has already been created (determined using the guid) compared to the source it skips it and does not make any changes.

However, in a blog type application, ideally it would instead of checking to see if exists, check to see if it changed (namely the :summary portion is what I'm after), maybe using the last_modified accessor and then update the already created record. Is there something similar to exists? that I could use to let an entry be updated like this/ am I on the right track or do I need to dig much deeper?

Was it helpful?

Solution

You can try changed?.

Idea is following (for existed records): set attributes without saving and check if entry.changed?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top