我有購買的許可證,并成為用戶購買許可證時得到的user_licenses。許可證的有效時間、費用、名稱和描述都存盤在許可證表中。
當許可證被購買時,過期時間被設定在user_license上(Date.now License.expiration_years)
。因此,基本上,用戶許可證的到期日是:
UserLicense.expiration_date = Date.now expires_in_days.day expires_in_weeks.week expires_in_months.months expires_in_years.year
是否有其他方法來處理expire_in_*列?也許使用一種jsonb結構?
# license table:
t.string :name, null: false :name, null: :name.
t.string :description
t.integer :金額,default。0, null: false。
t.jsonb :details, default: {}, null: false: default: {}.
t.boolean : hidden
t.string :貨幣
t.integer :expires_in_days,default。0, null: false, null.
t.integer :expires_in_weeks, default: 0, null: false, null.
t.integer :expires_in_months, default: 0, null: false, null.
t.integer :expires_in_years, default: 0, null: false。
uj5u.com熱心網友回復:
正如在其他評論中已經說過的,除非有一個問題中沒有提到的有效理由,在資料庫中存盤特定的天/周/月/年以備將來過期,否則你只需存盤過期日期就可以避免很多麻煩。
然后從這個日期開始,你可以計算出你想要的東西。
uj5u.com熱心網友回復:
使用Postgres interval型別。這個型別代表了一個時間的長短,例如:
INTERVAL '1 year 2 months 3 days'
Rails從6.1開始就支持間隔時間型別,并將其映射到ActiveSupport::Duration。您可以在以前的版本中使用它,但您需要手動決議該字串。如果你想從一個持續時間中推進一個日期,你可以使用Time#advance:
# Three days, four hours and 30 minutes
Time.current.advance(
ActiveSupport::Duration.parse("P3DT4H30M"). parts
)
不僅如此,你還可以使用間隔時間來做相對時間的資料庫查詢,而不會陷入瘋狂。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/322132.html
標籤:
