如何使用名为“ document”的表生成模型,其列为id:int,file:blob,status:enum,exp_date:datetime?

问题描述 投票:0回答:1

我在ActiveStorage中使用Rails 6。我有一个用户,签名者,签名,Signature_template,位置和文档的表。我有一个表也名为“ signature_template”,其列为user_id:int,file:blob(这是我对文档的问题),created_at:datetime和updated_at:datetime。

ruby-on-rails postgresql rails-activestorage
1个回答
0
投票

您可以在终端上创建

rails generate model Document 

然后按照以下步骤添加迁移文件

create_table :documents do |t|
  t.binary :file
  t.integer :status, default: 0
  t.datetime :exp_date
end

rails将自动创建id,因此您无需创建它

对于二进制类型,它取决于您的数据库系统,这里是映射对于一些著名的数据库系统

  • 数据库系统Mysql将被创建为blob,
  • Postgres-> bytea
  • SQLite-> blob
  • Oracle-> blob

对于枚举,您可以将其创建为整数,但随后在文档模型中您应提供以下信息(更改状态以符合您的需求)

class Document < ApplicationRecord
  enum status: %i(draft verified published)
...
end

这是您文档类型的导轨中列类型的一些详细信息

+-------------+---------------------------------------------------------------+
| column type |                        Description                            |
+-------------+---------------------------------------------------------------+
| :string     | Limited to 255 characters by default, Might be case-sensitive |
| :text       | Generally unlimited length depending on database              |
| :integer    | Whole number, in contrast to :decimal or :float.              |
| :decimal    | Stored with specified precision. Use for math accuracy.       |
| :float      | Floating-point decimal number with fixed precision            |
| :boolean    | True or false.                                                |
| :binary     | Raw chunks of data saved in database-specific way.            |
| :date       | Year, month and day (no time).                                |
| :time       | Hours, minutes, seconds (no date).                            |
| :datetime   | Date and time stored together.                                |
| :timestamp  | Exactly the same as :datetime on Rails.                       |
+-------------+---------------------------------------------------------------+
© www.soinside.com 2019 - 2024. All rights reserved.