Skip to content

Commit 5534733

Browse files
author
José Valim
committed
Merge pull request #6959 from robin850/patch-1
Add few information on the field types
2 parents 6c9c50c + 2f6004d commit 5534733

File tree

1 file changed

+49
-0
lines changed
  • railties/lib/rails/generators/rails/model

1 file changed

+49
-0
lines changed

railties/lib/rails/generators/rails/model/USAGE

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,55 @@ Description:
1919
then the generator will create a module with a table_name_prefix method
2020
to prefix the model's table name with the module name (e.g. admin_account)
2121

22+
Available field types:
23+
24+
Just after the field name you can specify a type like text or boolean.
25+
It will generate the column with the associated SQL type. For instance:
26+
27+
`rails generate model post title:string body:text`
28+
29+
will generate a title column with a varchar type and a body column with a text
30+
type. You can use the following types:
31+
32+
integer
33+
primary_key
34+
decimal
35+
float
36+
boolean
37+
binary
38+
string
39+
text
40+
date
41+
time
42+
datetime
43+
timestamp
44+
45+
You can also consider `references` as a kind of type. For instance, if you run:
46+
47+
`rails generate model photo title:string album:references`
48+
49+
It will generate an album_id column. You should generate this kind of fields when
50+
you will use a `belongs_to` association for instance. `references` also support
51+
the polymorphism, you could enable the polymorphism like this:
52+
53+
`rails generate model product supplier:references{polymorphic}`
54+
55+
You can also specify some options just after the field type. You can use the
56+
following options:
57+
58+
limit Set the maximum size of the field giving a number between curly braces
59+
default Set a default value for the field
60+
precision Defines the precision for the decimal fields
61+
scale Defines the scale for the decimal fields
62+
uniq Defines the field values as unique
63+
index Will add an index on the field
64+
65+
Examples:
66+
67+
`rails generate model user pseudo:string{30}`
68+
`rails generate model user pseudo:string:uniq`
69+
70+
2271
Examples:
2372
`rails generate model account`
2473

0 commit comments

Comments
 (0)