在 Ruby on Rails 中对资产/票据跟踪器建模
我对网络应用程序有一些想法。可悲的是,我对网络开发知之甚少。因此,我决定在完成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您基本上正在寻找
单表继承
又名STI
,请参阅此处的参考:http://api.rubyonrails.org/classes/ActiveRecord/Base.html
简而言之:
在您的情况下,每个设备都将从资产模型继承
You're basically looking for
Single Table Inheritance
akaSTI
, see reference here:http://api.rubyonrails.org/classes/ActiveRecord/Base.html
In a nutshell:
In your case, every device would inherit from an Asset model.
这是一个开源应用程序,我与其他一些人分叉并合作。它是一个使用多态性来表示资产的资产跟踪器,它并没有完全充实您想要做的事情,但查看源代码可能会对您有所帮助:
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