[英]Best way to represent reference data in Active Record
假設我有一個可以具有任何數量屬性的產品......並且這些屬性僅限於特定選項。 產品可以有(每個下面的有效選項):
這份名單可以繼續使用。 無論如何用單個表來表示活動記錄嗎? 我認為有一個看起來像這樣的單個參考數據表是理想的:
create_table :attritubes do |t|
t.string :name # Red, Blue, Green, Sports, Formal...
t.string :field_type # Color, Category, Waterproof...
end
為每個屬性建立一個模型似乎是浪費,因為它並沒有真正做多少。 這是一個我有一個名為屬性的通用模型的情況,產品有很多? 或者有一種方法可以讓活動記錄通過列來區分屬性(構成此語法):
belongs_to :color, class_name: :attribute, where(field_type: 'color')
輸入后,它幾乎看起來像一個范圍...是一個更好的方法去?
您可以為屬性創建單個表
create_table :attritubes do |t|
t.string :color
t.string :category
t.boolean :waterproof
end
然后在枚舉中獲取數據
enum color: { red:0, blue:1, green:2 }
enum category: { sports:0, formals:1, kids:2 }
enum waterproof: { no:0, yes:1 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.