回报率 - 在那里把一个自动化的过程 [英] RoR - where to put a automated process

查看:136
本文介绍了回报率 - 在那里把一个自动化的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的情况(ROR 3,红宝石1.9)。

following situation (ror 3, ruby 1.9).

我有一个模型的某些字段。

i've got a model with some fields.

现在我想要抓住一个JSON提要指标(如每隔一小时)比较我的存储模型对象的内容,并把一个新的模型对象如果在此JSON饲料的新项目。

now i want to grab a json feed (f.e. every hour) compare the content with my stored model objects and put in a new model object if there is a new item in this json feed.

我不是舒尔在哪里把这个自动操作。
我不认为这样一个模型的方法是正确的地方,对吗?

i'm not shure where to put this automated action. i don't think doing this in a models method is the right place, am i right?

您在哪里会做这种迁移行动?在控制器?不是真的,我认为。

where would you do this migration actions? in a controller? not really i think.

感谢所有提示

推荐答案

完全忽略这个控制器。模型作为用于访问数据的抽象层。你的情况,你有一个数据库的形式在本地的一些持久化的数据,你有你通过HTTP检索JSON作为远程数据。所以,你有你的正常的ActiveRecord模型,你有你的JSON数据模型。

Ignore controllers completely for this. Models serve as an abstraction layer for accessing data. In your case, you have some locally persisted data in the form of a database, and you have remote data you're retrieving as JSON over HTTP. So you have your regular ActiveRecord models, and you have a model for your JSON data.

让我们假设你有一个名为对RemoteData模型,获取一个JSON文件,有一些方法让你清晰地从它那里得到的数据。您也可以在以后显示的一直在你的数据库中检索内容的StoredData模式。

Let's assume you have a model called RemoteData that fetches a JSON document and has some methods for you to cleanly get data from it. You also have a StoredData model that keeps retrieved content in your database to be displayed later.

现在,要自动调用的过程 RemoteData.fetch('URL'),然后调用 StoredData.create:PARAMS 返回的数据。要做到这一点,你将创建一个rake任务。这里有一个例子:

Now, you want to automate the process of calling RemoteData.fetch('url') and then calling StoredData.create :params on the returned data. To do that, you would create a "rake task". Here's an example:

# lib/tasks/fetch.rake
desc "Fetch remote data and persist it"
task :fetch => :environment do
  RemoteData.fetch('url').each do |json_data|
    sd = StoredData.create :url => 'url', :data => json_data
    puts "Retrieved 'url' and saved data in record ##{sd.id} at #{DateTime.now}."
  end
end

然后你会设置你的系统的crontab到运行频繁,你想。 rake任务处理业务逻辑,你的模型处理与数据交互。漂亮和干净。

Then you would set your system crontab up to run that as frequent as you would like. The Rake task handles the business logic, and your models handle interacting with the data. Nice and clean.

这篇关于回报率 - 在那里把一个自动化的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆