ruby-on-rails - rails 4-- 没有 table 的模型

标签 ruby-on-rails activerecord ruby-on-rails-4

我有一个没有任何“本地”数据的 View ( Feed )的模型、 View 和 Controller 。也就是说,数据通过对客户端站点的 Web 服务调用获取并在本地 View 中呈现。

本地站点需要验证从远程站点抓取的对象是否有效(通过验证拥有 UserFeed 具有有效帐户),然后显示它。

我有逻辑从 Feed 中的远程提要中获取数据模型。然后是Feed Controller 获取提要 ID 的 URL 参数,将其传递给模型 getFeed功能,并显示它。

但是,由于没有表,Feed.getFeed(params[:feed_id]) Controller 中的调用返回错误信息 there is no table Feed .

我应该删除 Feed模型并制作 getFeed函数是辅助函数?处理没有表的模型的正确 Rails 方法是什么?

最佳答案

仅仅因为您使用的是 Rails,并不意味着每个模型都必须继承 ActiveRecord::Base
在您的情况下,我只是按照以下方式做一些事情:

# app/models/feed.rb
class Feed
  def initialize(feed_id)
    @feed_id = feed_id
  end

  def fetch
    # ... put the web-service handling here
  end

  # encapsulate the rest of your feed processing / handling logic here
end

# app/controllers/feeds_controller.rb
class FeedsController < ActionController::Base
  def show
    @feed = Feed.new(params[:feed_id])
    @feed.fetch

    # render
  end
end

请记住:它是 ruby 在 Rails 上,而不仅仅是 Rails。您可以做的不仅仅是 ActiveRecord 模型。

关于ruby-on-rails - rails 4-- 没有 table 的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024181/

相关文章:

ruby-on-rails - 使用 ActiveRecord 在 rails 3 中 inverse_of 的限制是什么

mysql - Rails/Mysql2/Lion [致命] 分配内存失败

php - Codeigniter 模型未保存在数据库中

ruby-on-rails - Facebook Messenger 机器人 : postback. 回复 webhook 与数据库查询不匹配

ruby-on-rails - 将属性附加到模型以在 Rails 3 上临时使用

ruby-on-rails - 使用 Arel 对 AND 和 OR 子句进行分组

ruby-on-rails - Rails 4.0 运行时错误 : @controller is nil: make sure you set it in your test's setup method

jquery - 检索 View 中选择选项的值

ruby-on-rails-4 - Sidekiq 警告 : uninitialized constant ViewDBWorker Rails 4

ruby-on-rails - 更改索引页 - Ruby on Rails