【发布时间】:2020-05-14 08:56:52
【问题描述】:
我在 Rails 中创建了一个模型,我需要一些与模型的每个实例相关的日期和时间属性。我无法找到 rails 提供的所有类型的字段,如字符串、布尔值、文本等?谁能给我一个链接,这将是一个很大的帮助。
【问题讨论】:
标签: ruby-on-rails ruby rubygems ruby-on-rails-5
我在 Rails 中创建了一个模型,我需要一些与模型的每个实例相关的日期和时间属性。我无法找到 rails 提供的所有类型的字段,如字符串、布尔值、文本等?谁能给我一个链接,这将是一个很大的帮助。
【问题讨论】:
标签: ruby-on-rails ruby rubygems ruby-on-rails-5
要查看有关模型的可用帮助,您可以运行命令rails g model。
命令和模型有很多细节。以下是关于字段类型的摘录,希望对您有所帮助。
可用的字段类型:
Just after the field name you can specify a type like text or boolean.
It will generate the column with the associated SQL type. For instance:
`rails generate model post title:string body:text`
will generate a title column with a varchar type and a body column with a text
type. If no type is specified the string type will be used by default.
You can use the following types:
integer
primary_key
decimal
float
boolean
binary
string
text
date
time
datetime
You can also consider `references` as a kind of type. For instance, if you run:
`rails generate model photo title:string album:references`
It will generate an `album_id` column. You should generate these kinds of fields when
you will use a `belongs_to` association, for instance. `references` also supports
polymorphism, you can enable polymorphism like this:
`rails generate model product supplier:references{polymorphic}`
For integer, string, text and binary fields, an integer in curly braces will
be set as the limit:
`rails generate model user pseudo:string{30}`
For decimal, two integers separated by a comma in curly braces will be used
for precision and scale:
`rails generate model product 'price:decimal{10,2}'`
【讨论】: