不同类型的模型字段在轨道上的红宝石?

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

我正在用Rails创建模型,我需要一些与模型的每个实例相关的日期和时间属性。我找不到导轨提供的所有类型的字段,例如字符串,布尔值,文本等?有人可以为我提供一个链接,这将有很多帮助。

ruby-on-rails ruby rubygems ruby-on-rails-5
1个回答
0
投票

要查看模型的可用帮助,可以运行命令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}'`
© www.soinside.com 2019 - 2024. All rights reserved.