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

问题描述

我想用我的Rails应用程序,检索从外部API的信息模型。我想这样做的是访问我的数据模型(其中可能包括来自多个API调用产生的信息),以类似于ActiveRecord模型将提供一种方法(特别协会和链能够查询方法相同的风格)。

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无表模型和博客帖子的),并看着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.

是含有一些,当我试图写的code的要点重新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.

您可以重新present外部API的应用程序中实例化类。

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

def 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.

看看一个API抽象我做了交通局饲料管。

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

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

10-28 02:17