关联两个模型并使用 Rails 中的单个控制器访问它们

发布于 2024-09-14 08:48:25 字数 2419 浏览 4 评论 0原文

背景:我正在 Rails 应用程序中使用 feedzirra 插件,我试图在单个视图中使用已解析提要的提要和条目访问器。

为了实现这一目标,我创建了两个模型:feed 和 feed_entry。它们如下:

feed.rb:

class Feed < ActiveRecord::Base
  attr_accessible :title, :url, :feed_url, :last_modified, :guid
  has_many :feed_entries

  def self.update_from_feed(feed_url)
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    add_feed(feed)
    FeedEntry.add_entries(feed.entries)
  end


  private

  def self.add_feed(feed)
    unless exists? :guid => feed.id
      create!(
        :title          => feed.title,
        :url            => feed.url,
        :feed_url       => feed.feed_url,
        :last_modified  => feed.last_modified,
        :guid           => feed.id
      )
    end
  end
end

feed_entry.rb:

class FeedEntry < ActiveRecord::Base
  attr_accessible :title, :url, :author, :summary, :content, :published, :guid
  belongs_to :feed

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

其目的是将 Feed.update_from_feed(feed_url) 作为 cron 作业调用。实际上,我希望让 cron 作业通过传入多个 feed 来调用这个函数;但我想我应该先让一个工作起来。为了进行测试,我目前正在从控制台调用此函数。

问题:

NoMethodError: undefined method `id' for #<Feedzirra::Parser::RSS:0x00000100bcb0f0>
  1. 这可能看起来很简单,但是在调用 update_from_feed 函数时,我在控制台中收到上述错误。虽然对 Ruby 和 Rails 很陌生,但我的印象是表中的每个新条目都会被赋予一个可以引用的 id - 在这种情况下,通过 feed.id 进行引用。我在这里遗漏了什么吗?

  2. 我使用此插件的 2 模型方法是一个好的方向吗?由于在解决问题 1 之前我无法验证其余方法是否有效,因此我想我会同时请求有关最佳实践的提示。

  3. 我的计划的其余部分是在单个控制器中利用插件的提要和条目访问器,然后以某种方式在相对视图中引用这些变量。这就是我应该如何看待 MVC 架构并以 RESTful 方式使用它吗?如果是这样,我如何在 FeedReader 视图中引用 feed 和条目访问器?

FeedReader 控制器如下所示:

class FeedEntriesController < ApplicationController
  def index
    @feed_entries = FeedEntry.all
    @feeds = Feed.all
  end
end

我知道这是一篇很糟糕的文章,但任何帮助将不胜感激。

Background: I am playing around with the feedzirra plugin in a rails app, and I am attempting to utilize both the feed and entry accessors of the parsed feed in a single view.

To accomplish this, I have created two models: feed and feed_entry. They are as follows:

feed.rb:

class Feed < ActiveRecord::Base
  attr_accessible :title, :url, :feed_url, :last_modified, :guid
  has_many :feed_entries

  def self.update_from_feed(feed_url)
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    add_feed(feed)
    FeedEntry.add_entries(feed.entries)
  end


  private

  def self.add_feed(feed)
    unless exists? :guid => feed.id
      create!(
        :title          => feed.title,
        :url            => feed.url,
        :feed_url       => feed.feed_url,
        :last_modified  => feed.last_modified,
        :guid           => feed.id
      )
    end
  end
end

feed_entry.rb:

class FeedEntry < ActiveRecord::Base
  attr_accessible :title, :url, :author, :summary, :content, :published, :guid
  belongs_to :feed

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

Where the intention is to call Feed.update_from_feed(feed_url) as a cron job. In reality, I'm hoping to have the cron job call this function by passing in multiple feeds; but I figured I'd get one working first. For sake of testing, I'm currently calling this function from the console.

Questions:

NoMethodError: undefined method `id' for #<Feedzirra::Parser::RSS:0x00000100bcb0f0>
  1. This may seem very simple, but in calling the update_from_feed function, I get the above error in my console. While new to ruby and rails, I was under the impression that every new entry in a table is given an id that can be referenced - in this scenario, by feed.id. Am I missing something here?

  2. Is my 2-model approach to using this plugin a good direction to go in? Since I'm not able to verify the rest of my approach works until I puzzle out question 1, I thought I'd ask for a tip on best practice in the mean time.

  3. The rest of my plan is to utilize the feed and entry accessors of the plugin within a single controller, and then reference those variables within the relative view somehow. Is this how I should be perceiving the MVC architecture and using it in a RESTful way? If so, how do I reference the feed and entry accessors in the FeedReader view?

This is what the FeedReader controller would look like:

class FeedEntriesController < ApplicationController
  def index
    @feed_entries = FeedEntry.all
    @feeds = Feed.all
  end
end

I know this was a bitch of a post, but any help would really be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-09-21 08:48:25

您的方法存在一些缺陷。

首先,当你更新你的 feed 时,你会检查是否存在(很好),但不会处理它存在的可能性,然后你无论如何都会尝试收集新的 feed。你需要类似的东西;

feed = Feed.find_by_url(incoming_url) # try loading an existing feed first
feed = Feedzirra::Feed.fetch_and_parse(feed_url) if !feed # if not, create a new one

假设最后一个 Feedzirra 命令是创建新提要的命令。我不记得了!

两种模型的想法似乎是一种明智的方法。稍后通过控制器访问提要时,您可能应该尝试嵌套资源。那么你的 URL 看起来会是这样的;

/feed/1/entries

你的控制器会是这样的;

@feed = Feed.find(params[:feed_id])
@entries = @feed.entries

我还没有回答你所有的问题,但希望这是一个开始。

There are a couple of flaws in your approach.

Firstly, when you update your feed, you are checking whether or not exists (great) but then not dealing with the possibility that it does and then you try to collect new feeds anyway. You need something like;

feed = Feed.find_by_url(incoming_url) # try loading an existing feed first
feed = Feedzirra::Feed.fetch_and_parse(feed_url) if !feed # if not, create a new one

Assuming that the last Feedzirra command is the one to create a new feed. I can't remember!

The two model idea seems like a sensible approach. When accessing your feeds via the controllers later, you should probably try nested resources. Then your URL would look something like;

/feed/1/entries

And your controller would be something like;

@feed = Feed.find(params[:feed_id])
@entries = @feed.entries

I haven't answered all your questions, but hopefully it's a start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文