簡體   English   中英

Rails 模型字段值

[英]Rails Model field value

我在 Rails 中有一個名為 Estimation 的模型,它有一個名為 request_type_id 的字段。 RequestType 本身就是一個模型。 Estimation 類的所有對象的 request_type 字段的可能值是固定的(中、低、高)。

RequestType 模型有一個自動生成的 id 和 Name 作為可用字段。 我打算在模型中插入三個值,low、medium 和 high,這些值的 ID 為 1,2 和 3。那么下面的類設計是否正確?

class RfsEstimation < ActiveRecord::Base
has_one :request_type

如果您想為特定屬性保持固定值,那么將該特定列定義為 enum 總是好的

例如:- 添加request_type作為具有整數數據類型的RfsEstimation模型的列或屬性之一。

class RfsEstimation < ActiveRecord::Base
  enum request_type: { low: 1, medium: 2, high: 3 }
end

由於 RfsEstimation 持有 RequestType 的外鍵,因此您需要使用belongs_to關聯。

class RfsEstimation < ActiveRecord::Base
belongs_to :request_type

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM