处理 Rails 中的遗留数据库视图

发布于 2024-10-19 22:17:14 字数 539 浏览 1 评论 0原文

我是 ruby​​ 和 Rails 的新手,我很难将 MVC 技术与数据库视图结合起来进行概念化。我正在处理一个遗留数据库,它有多个用于生成报告的视图。

我迷失的是如何实际使用数据库视图。是否应该将其放入模型中?如果是这样的话,那到底是什么样子的?

例如,旧数据库有一个名为 qryTranscriptByGroup 的视图。它在旧应用程序中的 SQL 语句中使用,例如“SELECT * FROM qryTranscriptByGroup WHERE group='test_group'”。这会返回少量记录,通常少于 100 条。

如果我创建一个模型 Transcript,我将如何定义像 Transcript.find_by_group(group) 这样的方法?同样,我似乎可能需要阻止任何其他“查找”方法,因为它们在这种情况下是无效的。

还有一个事实是,该视图是只读的,我需要阻止任何创建、更新或销毁它的尝试。

也许我的处理方式完全错误。最重要的是,我需要从几个表示用户信息(记录)的表(模型?)中获取信息。实际上是一个或多个用户(成绩单复数)。

-谢谢!

I am new to ruby and rails and I am having difficulty conceptualizing the MVC techniques in conjunction with database views. I am dealing with a legacy database that has several viiews that are used to generate reports.

Where I get lost is how do I actually use a database view. Should it be put in a model? If so what exactly would that look like?

As an example the legacy db has a view called qryTranscriptByGroup. It is used in the legacy application in an SQL statement such as "SELECT * FROM qryTranscriptByGroup WHERE group='test_group'". This returns a small number of records usually less than 100.

If i create a model, Transcript, how would I define a method like Transcript.find_by_group(group)? As well, it would seem that I might need to prevent any other "find" methods as they would be invalid in this context.

There is also the the fact that the view is read-only and I would need to prevent any attempts to create, update or destroy it.

Perhaps I am going about this entirely the wrong way. The bottom line is that I need to get information from several tables (models?) that represent the information about a user (a transcript). Actually one or more users (transcripts plural).

-Thanks!

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

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

发布评论

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

评论(1

灼疼热情 2024-10-26 22:17:14

您可以像普通模型一样使用数据库视图。

在您的情况下:

class Transcript < ActiveRecord::Base
  set_table_name "qryTranscriptByGroup"
  set_primary_key "if_not_id"
end

查询将是:

Trascript.find_by_group('test_group')

无需声明任何内容。

Rails 使用 method_missing 方法神奇地生成 find_by_column_name 方法。

对于创建/更新/删除操作,您可以简单地删除它们或不在控制器中创建它们。

You can use a database view like a normal model.

In your case:

class Transcript < ActiveRecord::Base
  set_table_name "qryTranscriptByGroup"
  set_primary_key "if_not_id"
end

The query will be then:

Trascript.find_by_group('test_group')

without you need to declare anything.

Rails uses the method_missing method to magically generate find_by_column_name methods.

For the create/update/delete action you can simply delete them or not create them in the controller.

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