我需要将非数据库数据类创建为 Rails 3“模型”,还是只是 /lib 区域中的基本类?

发布于 2025-01-08 06:02:52 字数 206 浏览 0 评论 0原文

我需要将非数据库数据类创建为 Rails 3“模型”,还是只是 /lib 区域中的基本类?

我想构建一些类来表示计算的域对象。因此这些字段不会引用数据库表/列。类中还会有处理数据的方法。

问题 - 生成这些类时,这些类应该只是我放在 /lib 区域中的普通 Ruby 类吗?或者我/我应该使用 Rails 模型(并使用“rails g model ...”生成)吗?

should a non-database data class I need be created as a Rails 3 "model", or just a basic class in the /lib area?

I want to build some classes to represent calculated domain objects. So the fields won't reference a database table/column. There will be also methods in the class that with the data.

Question - When generating these classes should these be just normal Ruby classes I put in the /lib area? Or should I/can I use rails models for this (and generate with "rails g model ...")?

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

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

发布评论

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

评论(3

ぇ气 2025-01-15 06:02:52

无表模型可能应该保存在应用程序/模型中。我有几个可以访问 API 的。 Rails 中的 ActiveModel 可以帮助引入 Active Record 的一些有用功能。

Tableless models should probably be kept in app/models. I have several that access APIs. ActiveModel in Rails can help bring in some of the useful functionality of Active Record.

合约呢 2025-01-15 06:02:52

有趣的问题..几周前我也有同样的问题。

我将这些类放在 model 目录下。这就是我做出这个决定的方式。我编写的类与特定应用程序完全相关,至少目前没有可以与任何人共享的共同功能。我还需要使用一些现有的模型来查询该类中的一些数据。所以我把它作为一个类放在模型目录下。我可能是错的,但这就是我现在所做的。

在另一种情况下,我为网络和移动应用程序使用特定的 api 集,我正在考虑将与 api 交互的代码制作成 gem。这里需要注意的是,API 集也是我系统的一部分,并且只会由我的应用程序使用。

Interesting question..I had the same question a few weeks back.

I put those class under model directory. This is how I came up with that decision. The class that I wrote was completely related to a particular app no common features to share with anyone at-least at the moment. I also needed to use some of my existing models to query some data in that class. So I made it a class under model directory. I might be wrong but thats what I have done now.

In another case where I am using a certain api sets for a web and mobile app I am thinking of making the code which interfaces with the api into a gem. The thing to note here is that the api set is also part my system and it will only be used by my apps.

好久不见√ 2025-01-15 06:02:52

未映射到数据库表的类仍然可以驻留在应用程序/模型中。您可以简单地声明不带任何扩展(或您自己的扩展)的类,而不是从 ActiveRecord::Base 扩展您的类。

CanCan 能力模型就是一个很好的例子。它驻留在 app/models/ 中,但不扩展 ActiveRecord::Base。有关 CanCan 能力模型的更多信息,请访问:https://github.com/ryanb/ cancan/wiki/Defining-Abilities

还要考虑到 lib/ 下的代码默认情况下不会在开发环境中重新加载。

Classes that don't map to database tables can still reside inside of app/models. Instead of extending your class from ActiveRecord::Base, you can simply declare your class without any extensions (or your own extensions).

The CanCan ability model is a good example of this. It resides in app/models/, however does not extend ActiveRecord::Base. More information on CanCan's ability model can be found here: https://github.com/ryanb/cancan/wiki/Defining-Abilities

Also consider that code under lib/ will not reload in the development environment by default.

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