forked from MushroomObserver/taxoneval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.doc
130 lines (93 loc) · 4.78 KB
/
model.doc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Usage:
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]
Options:
[--skip-namespace] # Skip namespace (affects only isolated applications)
-o, --orm=NAME # Orm to be invoked
# Default: active_record
ActiveRecord options:
[--migration] # Indicates when to generate migration
# Default: true
[--timestamps] # Indicates when to generate timestamps
# Default: true
[--parent=PARENT] # The parent class for the generated model
[--indexes] # Add indexes for references and belongs_to columns
# Default: true
-t, [--test-framework=NAME] # Test framework to be invoked
# Default: rspec
Rspec options:
[--fixture] # Indicates when to generate fixture
[--fixture-replacement=NAME] # Fixture replacement to be invoked
# Default: factory_girl
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Suppress status output
-s, [--skip] # Skip files that already exist
Description:
Stubs out a new model. Pass the model name, either CamelCased or
under_scored, and an optional list of attribute pairs as arguments.
Attribute pairs are field:type arguments specifying the
model's attributes. Timestamps are added by default, so you don't have to
specify them by hand as 'created_at:datetime updated_at:datetime'.
You don't have to think up every attribute up front, but it helps to
sketch out a few so you can start working with the model immediately.
This generator invokes your configured ORM and test framework, which
defaults to ActiveRecord and TestUnit.
Finally, if --parent option is given, it's used as superclass of the
created model. This allows you create Single Table Inheritance models.
If you pass a namespaced model name (e.g. admin/account or Admin::Account)
then the generator will create a module with a table_name_prefix method
to prefix the model's table name with the module name (e.g. admin_account)
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. You can use the following types:
integer
primary_key
decimal
float
boolean
binary
string
text
date
time
datetime
timestamp
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 this kind of fields when
you will use a `belongs_to` association for instance. `references` also support
the polymorphism, you could enable the 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}`
You can add a `:uniq` or `:index` suffix for unique or standard indexes
respectively:
`rails generate model user pseudo:string:uniq`
`rails generate model user pseudo:string:index`
You can combine any single curly brace option with the index options:
`rails generate model user username:string{30}:uniq`
`rails generate model product supplier:references{polymorphic}:index`
Examples:
`rails generate model account`
For ActiveRecord and TestUnit it creates:
Model: app/models/account.rb
Test: test/models/account_test.rb
Fixtures: test/fixtures/accounts.yml
Migration: db/migrate/XXX_create_accounts.rb
`rails generate model post title:string body:text published:boolean`
Creates a Post model with a string title, text body, and published flag.
`rails generate model admin/account`
For ActiveRecord and TestUnit it creates:
Module: app/models/admin.rb
Model: app/models/admin/account.rb
Test: test/models/admin/account_test.rb
Fixtures: test/fixtures/admin/accounts.yml
Migration: db/migrate/XXX_create_admin_accounts.rb