我应该在模型或控制器中定义导出吗?

发布于 2024-11-05 06:28:48 字数 191 浏览 1 评论 0原文

我有一个 Rails 应用程序需要导出数据。数据导出是一项复杂的操作,它创建包含来自各种模型的信息的外部数据库。我目前将数据导出例程编写为单独的 rake 任务,现在我也想将它们公开给用户。

我不知道是否要创建一个包含所有导出逻辑的模型,因此本质上该模型将代表单个导出实例,或者我应该将例程创建为辅助库并直接从控制器使用它,这在某种程度上也是有道理的。

I have an Rails app that needs to export data. The data export is a complex operation that creates an external database that contains information from various models. I have the data export routines written as a separate rake task at the moment and now I want to expose them to the user as well.

What I can't figure out is whether to create a model that contains all the logic for the export, so essentially the model would represent a single export instance or should I create the routines as a helper library and use that directly from a controller, which in a way makes sense too.

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

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

发布评论

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

评论(2

绅士风度i 2024-11-12 06:28:48

基本上,“导出”只是创建另一种访问数据的方式。

管理访问和表示数据的方式的是视图。所以你应该只创建另一个视图,而不是模型或控制器。

当然,您将创建的视图将调用某种导出功能。导出函数将数据模型作为输入并以“导出”形式返回数据。这可能是模型对象中的方法(“export me”),也可能是 lib/ 文件夹中脚本中的单独函数。

但是,当您实现此导出功能时,您可能希望使用其他数据模型(例如,缓存导出的数据)。但这是导出函数的实现细节,可能根本不需要。

Basically, "exporting" is just creating another way to access data.

What manages ways to access and to represent data is the view. So you should just create another view, not a model or a controller.

The view you'll create, of course, will call some kind of exporting functionality. The exporting function takes a data model as an input and returns data in the "exported" form. This may be either a method in your model object ("export me"), or a separate function residing in a script in your lib/ folder.

However, when you implement this exporting function, you might want to use another data model (for instance, to cache the exported data). But that's an implementation detail of the exporting function, and it may not be needed at all.

淡忘如思 2024-11-12 06:28:48

以我的拙见,您似乎应该创建一个模块或类(无论哪个更适合您的导出实现),并从控制器调用它。对我来说,似乎用户正在发出请求,然后控制器将该请求定向到外部库来完成它,然后外部库可以向控制器报告其完成情况,并且控制器可以通知视图。

仅当您要将导出请求存储在数据库中时,将其放入模型才对我有意义。

In my humble opinion, it seems like you should create a module or class (whichever is more appropriate for your export implementation), and call that from the controller. To me, it seems like a user is making a request, and the controller is then directing that request to the external library to fulfill it, which can then report back to the controller its done, and the controller can inform the view.

Putting it in a model only makes sense to me if you're going to store the requests for export in the database.

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