在 Rails 中实施租赁商店:如何跟踪一段时间内的库存状态?
假设您正在为一家滑雪板租赁商店实施 Rails 应用程序。
给定的滑雪板可以处于 3 种状态之一:
- 外出维修
- 可在商店 X
- 借给客户 Y
公司需要能够查看
- 的特定滑雪板
- 特定客户
的租赁历史记录租赁历史记录需要包括时态数据 (例如,Sally于2009年12月1日至2009年12月3日租用了滑雪板0123)。
你会如何设计你的模型?您是否有一个包含 4 列(id、state、customer、store)的滑雪板表,并在每次状态更改时将此表中的行以及时间戳复制到 Snowboard_history 表中?
谢谢!
(注意:我实际上并不是在尝试实现租赁商店;这只是我能想到的最简单的类比。)
Let's say you're implementing rails app for a snowboard rental store.
A given snowboard can be in one of 3 states:
- away for maintenance
- available at store X
- on loan to customer Y
The company needs to be able to view a rental history for
- a particular snowboard
- a particular customer
The rental history needs to include temporal data (e.g. Sally rented snowboard 0123 from Dec. 1, 2009 to Dec. 3 2009).
How would you design your model? Would you have a snowboard table with 4 columns (id, state, customer, store), and copy rows from this table, along with a timestamp, to a snowboard_history table every time the state changes?
Thanks!
(Note: I'm not actually trying to implement a rental store; this was just the simplest analogue I could think of.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用一对插件来完成工作。这将使用四个模型。滑雪板、商店、用户和审计。
acts_as_state_machine 和 acts_as_audited
AASM 简化了状态转换。审核时会创建您想要的历史记录。
Store 和 User 的代码很简单,acts_as_audited 将处理审计模型。
假设您的客户是状态更改时控制器操作期间的 current_user,这就是您所需要的。
获取滑雪板历史记录:
获取客户的租赁历史记录:
您可能需要创建一个辅助方法来将客户的历史记录转化为更有用的内容。也许像他的那样:
I would use a pair of plugins to get the job done. Which would use four models. Snowboard, Store, User and Audit.
acts_as_state_machine and acts_as_audited
AASM simplifies the state transitions. While auditing creates the history you want.
The code for Store and User is trivial and acts_as_audited will handle the audits model.
Assuming your customer is the current_user during the controller action when state changes that's all you need.
To get a snowboard history:
To get a customer's rental history:
You might want to create a helper method to shape a customer's history into something more useful. Maybe something like his:
首先,我将为滑雪板、客户和商店生成单独的模型。
(rails 自动生成
id
和created_at
、modified_at
日期)为了保留历史记录,我不会从这些表中复制行/值,除非这是必要的(例如,如果您想跟踪客户租用的价格)。
相反,我会使用您描述的类似属性创建 SnowboardEvent 模型(如果您愿意,您可以将其称为
SnowboardHistory
,但就个人而言,创造新的历史感觉很奇怪):ev_type
(即 0 表示退货,1 表示维护,2 表示租赁...)snowboard_id
(不为空)customer_id
store_id
例如,
然后我设置
SnowboardEvent
、Snowboard
、Customer
和Store
之间的所有关系。 Snowboard 可以具有current_state
、current_store
等功能,并且客户将具有相同的
has_many :snowboard_events
。检查滑雪板或客户历史记录只需使用
Snowboard.snowboard_events
或Customer.snowboard_events
循环记录即可。 “时间数据”将是这些事件的created_at
属性。我不认为使用观察者是必要的或相关的。注意:上面的代码没有经过测试,也绝不是完美的,只是为了得到这个想法:)
First I would generate separate models for Snowboard, Customer and Store.
(rails automatically generates
id
andcreated_at
,modified_at
dates)To preserve the history, I wouldn't copy rows/values from those tables, unless it is necessary (for example if you'd like to track the price customer rented it).
Instead, I would create SnowboardEvent model (you could call it
SnowboardHistory
if you like, but personally it feels strange to make new history) with the similiar properties you described:ev_type
(ie. 0 for RETURN, 1 for MAINTENANCE, 2 for RENT...)snowboard_id
(not null)customer_id
store_id
For example,
Then I'd set all the relations between
SnowboardEvent
,Snowboard
,Customer
andStore
. Snowboard could have functions likecurrent_state
,current_store
implemented asAnd Customer would have same
has_many :snowboard_events
.Checking the snowboard or customer history, would be just a matter of looping through the records with
Snowboard.snowboard_events
orCustomer.snowboard_events
. The "temporal data" would be thecreated_at
property of those events. I don't think using Observer is necessary or related.NOTE: the above code is not tested and by no means perfect, but just to get the idea :)