Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to specify limit while generating a model

in rails, we can generate the model like this

rails generate model post title:string body:text published:boolean

And the command has following syntax as per the rails guide

$ rails generate model
Usage: rails generate model NAME [field:type field:type] [options]

I am wondering if its possible to specify limit/length for each field as part of [options]? Further, what is the use of [options] parameter?

like image 587
CuriousMind Avatar asked Jun 03 '12 20:06

CuriousMind


3 Answers

You can provide the limit with braces:

rails g model Session session_id:string{40} user_agent:string{200}
like image 77
chris finne Avatar answered Nov 28 '22 07:11

chris finne


In rails 5 I had to use rails g model User name:"string{16}"

like image 34
Akash Agarwal Avatar answered Nov 28 '22 05:11

Akash Agarwal


You need to give the size in the curly braces, which will get transformed into limits

rails generate model MyModel some_id:integer{20} some_name:string{255}
some_text:text some_int:integer{1} some_deci:decimal{10,2}

You can also have other data types such as boolean, date, time, datetime, float, binary etc..

Options are something like 'do you also need to run migration' or not.

like image 28
Vasanth Saminathan Avatar answered Nov 28 '22 05:11

Vasanth Saminathan