简体   繁体   English

Rails:如何为以下逻辑建模

[英]Rails: How to model following logic

I have a model called Item, which belongs to Report. 我有一个名为Item的模型,它属于Report。

class Item < ApplicationRecord
 belongs_to :report
end

For my Item, I want to have an attribute called item_category(besides other attributes). 对于我的商品,我想要一个名为item_category的属性(除其他属性之外)。

t.string "item_category"

I will have some standard categories that will have a tax associated with them. 我将有一些与税相关的标准类别。

My question is: Should I have another two models for Category and Tax, and load the available categories that will be created as options for my Item category attribute ? 我的问题是:我是否应该再有两个用于Category和Tax的模型,并加载将作为我的Item category属性的选项创建的可用类别? (or Tax should be an attribute for a Category model, or none of them) (或Tax应该是Category模型的一个属性,或者都不是)

After some time, another category can be added/removed, same for the Tax and I want this to be as modular as possible, not to make changes every time I want to remove/add category/tax. 一段时间后,可以添加/删除另一个类别,与Tax相同,我希望此类别尽可能模块化,而不是每次要删除/添加类别/税收时都进行更改。 How should I approach it? 我应该如何处理?

It really depends on how frequently categories will change. 这实际上取决于类别更改的频率。 If tax + category pair is constant, then I would suggest adding that in code, as a constant (Hash, maybe, structured as { "category" : "tax" }). 如果tax + category对是常量,那么我建议在代码中将其添加为常量(哈希,结构化为{“ category”:“ tax”})。 If taxes will change often, then it makes more sense of making another Category model and storing your tax + Category stuff there. 如果税率经常变化,那么制作另一个“ Category模型并在其中存储税项和“类别”内容就更有意义了。

From your description it doesn't seem like you need a separate Tax model (I assume you will only have % amount there). 从您的描述看来,您似乎不需要单独的Tax模型(我假设您那里只会有%的金额)。 This is based on an assumption that Category has only one tax. 这是基于“类别”仅征收一种税的假设。

That is all I can suggest with the information you have provided. 根据您提供的信息,这就是我所能建议的。 But the most optimal methid laregly depends on more details description of your business logic 但是,最佳的方法通常取决于您的业务逻辑的更多详细描述

I suggest the model 我建议模型

  • Model: Categorytax 型号:Categorytax
    • category:string 类别:弦乐
    • taxProcent: integer taxProcent:整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM