在 Ruby on Rails 中对资产/票据跟踪器建模

发布于 2024-11-08 17:00:08 字数 532 浏览 0 评论 0原文

我对网络应用程序有一些想法。可悲的是,我对网络开发知之甚少。因此,我决定在完成 Michael Hartl 的 Ruby on Rails 教程。我选择这种应用程序是因为它是我熟悉的东西,所以我已经知道它应该如何运行。当我向自己的应用程序添加功能时,这样的系统还需要使用 Rails 的许多功能。

但是,我对建模有一些疑问:

计算机、打印机、显示器、手机都是一种资产,因此它们都会共享一些字段(购买日期、购买价格、保修期限、注释),但它们也会有一些基于资产类型的特定类型字段。从严格的 OOP 角度来看,我想对其进行“子类化”。我应该如何在 Rails 中执行此操作?

我想随机生成一个资产和票号;我应该使用该数字作为项目的主键,还是这是不好的做法?我这样做的想法是创建简单、有用的 CRUD URL。

可惜的是,每个“问题”我只能奖励一张支票;我想我会选择最佳的整体答案。任何见解表示赞赏。

I have some ideas for web apps. Sadly, I have very little knowledge of web development. So, I have decided to teach myself by building a "real word" example by in the form of an asset/ticket tracker after completing the Ruby on Rails Tutorial by Michael Hartl. I chose this kind of application because it is something that I am familiar with, so I already have an idea of how it should function. Such a system will also require me to use many of the features of Rails as I add features to my own application.

However, I have a few questions about modeling:

Computers, printers, monitors, cell phones are all a type of asset, so they would all share some fields (date purchased, purchase price, warranty length, notes), but they would also have some type specific fields based upon the type of asset. Looking from a strictly OOP perspective, I'd like to "sub class" it. How should I do this in Rails?

I want to randomly generate a asset and ticket numbers; should I use that number as the item's primary key, or is that bad practice? My thought in doing so would be to create simple, useful CRUD URLs.

It's too bad that I can only award one check per "question"; I guess I'll choose the best overall answer. Any insights are appreciated.

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

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

发布评论

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

评论(2

夜空下最亮的亮点 2024-11-15 17:00:08

您基本上正在寻找单表继承又名STI,请参阅此处的参考:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html

简而言之:

Active Record 允许继承
将类的名称存储在
默认情况下名为“type”的列
(可以通过覆盖来改变
Base.inheritance_column)。这意味着
继承看起来像这样:

类公司< ActiveRecord::基;
结束

类公司<公司;
结束

类客户端<公司;
结束

在您的情况下,每个设备都将从资产模型继承

You're basically looking for Single Table Inheritance aka STI, see reference here:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html

In a nutshell:

Active Record allows inheritance by
storing the name of the class in a
column that by default is named “type”
(can be changed by overwriting
Base.inheritance_column). This means
that an inheritance looking like this:

class Company < ActiveRecord::Base;
end

class Firm < Company;
end

class Client < Company;
end

In your case, every device would inherit from an Asset model.

单身情人 2024-11-15 17:00:08

这是一个开源应用程序,我与其他一些人分叉并合作。它是一个使用多态性来表示资产的资产跟踪器,它并没有完全充实您想要做的事情,但查看源代码可能会对您有所帮助:

https://github.com/mechcow/Asset-Tracker

This is an open source app I forked and worked on with a few other people. It is an asset tracker that uses polymorphism to represent assets, its not completely fleshed out for what you are trying to do but a look through the source may assist you:

https://github.com/mechcow/Asset-Tracker

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