Aaditya rathore
Aaditya rathore

Reputation: 93

Different types of Model fields in ruby on rails?

I was creating a model in rails and I need some attributes for date and time related to each instance of the model. I'm unable to find all types of fields the rails provide like string, boolean, text etc? Can somebody provide me a link for that, it would be a lot of help.

Upvotes: 0

Views: 898

Answers (1)

The_Cute_Hedgehog
The_Cute_Hedgehog

Reputation: 1340

To see available help on model you can run the command rails g model. There is a tons of details on the command and models. Here is an excerpt about the field types, hope it helps.

Available field types:

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}'`

Upvotes: 3

Related Questions