本文介绍了在 Ruby on Rails 中为由外部 API 支持的模型使用 ActiveRecord 接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Rails 应用程序中使用模型来从外部 API 检索信息.我想要做的是以类似于 ActiveRecord 模型将提供的方式(特别是关联和相同样式的可链式查询方法)访问我的数据模型(可能由多个 API 调用产生的信息组成).

I'm trying to use Models in my Rails application that retrieve information from an external API. What I would like to do is access my data models (which may consist of information resulting from multiple API calls) in a way similar to what an ActiveRecord model would provide (specifically associations, and the same style of chain-able query methods).

我最初的直觉是重新创建我想要的 ActiveRecord 部分并合并这个 API.不想重新发明轮子",也不想确切了解添加更多功能需要多少工作,这让我退后一步,重新评估如何解决这个问题.

My initial instinct was to recreate the parts of ActiveRecord that I wanted and incorporate this API. Not wanting to 'reinvent the wheel' and seeing exactly how much work would be required to add more functionality have made me take a step back and reevaluate how to approach this.

我找到了在没有表格的情况下使用 ActiveRecord 的方法(参见:Railscast #193 Tableless Model 和博客文章 此处) 并查看 ActiveRecord.因为 ActiveModel 似乎只包含验证,所以我不确定这在这种情况下是否有帮助.在没有表格的情况下使用 ActiveRecord 的解决方法似乎是最好的选择,但我怀疑有一种更简洁的方法可以做到这一点,但我没有看到.

I have found ways to use ActiveRecord without a table (see: Railscast #193 Tableless Model and the blog post here) and looked into ActiveRecord. Because ActiveModel only seems to include Validations I'm not sure that's very helpful in this situation. The workaround to using ActiveRecord without a table seems like the best option, but I suspect there's a cleaner way of doing this that I'm just not seeing.

这里是一个要点,其中包含我尝试重新创建 ActiveRecord 时编写的一些代码功能,大量借用 ActiveRecord 源本身.

Here is a gist containing some of the code written when I was trying to recreate the ActiveRecord functionality, borrowing heavily from the ActiveRecord source itself.

我的问题归结为:通过对上面指定的 ActiveRecord 实施变通方法或自己重新创建功能,我可以获得我想要的功能(链接查询方法、关系),但这些真的是理想的解决方案吗?

My question boils down to: I can get the functionality I want (chaining query methods, relations) by either implementing the workaround to ActiveRecord specified above or recreating the functionality myself, but are these really ideal solutions?

推荐答案

请记住,Rails 的底层仍然只是 Ruby.

Remember that Rails is still just Ruby underneath.

您可以将外部 API 表示为应用程序中的实例化类.

You could represent the external API as instantiated classes within your application.

class Event
  def self.find(id)
    #...External http call to get some JSON...#
    new(json_from_api)
  end

  def initialize(json)
    #...set up your object here...#
  end


  def attendees
    #...external http call to get some JSON and then assemble it 
    #...into an array of other objects
  end
end

因此,您最终会编写本地抽象来从 api 调用创建 ruby​​ 对象,您可能还可以将 ActiveModel 或 Virtus 混入其中,这样您就可以使用属性的哈希分配和表单验证等.

So you end up writing local abstractions to create ruby objects from api calls, you can probably also mix in ActiveModel, or Virtus into it, so you can use hash assignment of attributes, and validations for forms etc.

看看我为管的 TfL 提要所做的 API 抽象.service_disruption

Take a look at an API abstraction I did for the TfL feed for the tube. service_disruption

这篇关于在 Ruby on Rails 中为由外部 API 支持的模型使用 ActiveRecord 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:17