简体   繁体   中英

Accessing methods in other models ruby on rails

Having some trouble getting my head around accessing a method from another model. Can't seem to fix my code.

My ruby on rails application is a simple RSS reader. I have two models, Feeds and FeedEntries, I'm trying to loop though the urls in Feeds and pass them onto a method in FeedEntries which will phrase them and add them to the database. The latter is working find but having trouble accessing the update_from_feed method from my Feeds model here are my model's code:

class FeedEntry < ActiveRecord::Base
  def self.update_from_feed(feed_url)
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    feed.entries.each do |entry|
      unless exists? :guid => entry.id
        create!(
          :name         => entry.title,
          :summary      => entry.summary,
          :url          => entry.url,
          :published_at => entry.published,
          :guid         => entry.id
          )
      end
    end
  end
end

And here is my feeds model:

class Feed < ActiveRecord::Base
  def self.fetch_all
    feeds.each do |feed|
      FeedEntry.update_from_feed(feed.url)
    end
  end
end

When I jump into the console to try and run Feed.fetch_all I get:

NameError: undefined local variable or method `feeds'

I bet this is a really silly error, But I can't get my head around how to fix it? Thanks for reading.

All fixed! Feeds model should have looked like this:

class Feed < ActiveRecord::Base
  def self.fetch_all
    Feed.all.each do |feed|
      FeedEntry.update_from_feed(feed.url)
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM