Skip to content

Commit 00f7501

Browse files
authored
Update README.md
1 parent 27088a5 commit 00f7501

File tree

1 file changed

+50
-62
lines changed

1 file changed

+50
-62
lines changed

RoR/README.md

Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,56 @@ ActiveRecord::Base.connection.columns('users').map(&:name) # List all column
8787

8888
```
8989

90+
### Rails Migration File Naming Guide
91+
92+
#### Location
93+
All migration files are stored in the `db/migrate/` directory.
94+
95+
#### Filename Format
96+
97+
`YYYYMMDDHHMMSS_migration_name.rb`
98+
99+
**Examples**:
100+
- `20250402104500_create_users.rb`
101+
- `20250402110000_add_status_to_orders.rb`
102+
- `20250402113000_rename_email_in_customers.rb`
103+
104+
#### Class Name Format
105+
The migration class name should match the CamelCased version of the file’s suffix.
106+
107+
**Examples**:
108+
- `20250402104500_create_users.rb``class CreateUsers`
109+
- `20250402110000_add_status_to_orders.rb``class AddStatusToOrders`
110+
- `20250402113000_rename_email_in_customers.rb``class RenameEmailInCustomers`
111+
112+
> ⚠️ **Important**: If you rename the migration file, you **must update the class name** inside the file to match, or Rails will raise a `NameError` about a missing class.
113+
114+
#### Using the `change` Method
115+
116+
Rails migrations often use the `change` method instead of `up` and `down` for simpler reversible actions.
117+
Rails can **automatically reverse** the following methods:
118+
119+
- `add_column`
120+
- `add_index`
121+
- `add_timestamps`
122+
- `create_table`
123+
- `remove_timestamps`
124+
- `rename_column`
125+
- `rename_index`
126+
- `rename_table`
127+
128+
> For more complex migrations, use explicit `up` and `down` methods.
129+
130+
### Model
131+
132+
Student model represents a single student, so it's singular. The students table holds all of the students, so it's plural.
133+
134+
Create a model
135+
136+
```shell
137+
rails g model Student first_name:string last_name:string gender:string
138+
```
139+
90140
## Code Quality Tools
91141

92142
### RuboCop
@@ -136,68 +186,6 @@ bundle exec rspec
136186
annotate
137187
```
138188

139-
# Initialization
140-
141-
For macOS
142-
143-
Postgresql setup
144-
```
145-
brew install postgresql
146-
brew services start postgresql
147-
```
148-
149-
Create an app with postgresql
150-
151-
```
152-
rails new app --database=postgresql
153-
bin/rails db:migrate
154-
```
155-
156-
```
157-
rails g controller admin index
158-
```
159-
160-
161-
162-
# Database
163-
164-
Student model represents a single student, so it's singular. The students table holds all of the students, so it's plural.
165-
166-
Create a model
167-
168-
```
169-
rails g model Student first_name:string last_name:string gender:string
170-
```
171-
172-
Model validations
173-
174-
https://guides.rubyonrails.org/active_record_validations.html
175-
176-
https://learn.co/lessons/activerecord-lifecycle-reading
177-
178-
179-
List all tables
180-
```
181-
ActiveRecord::Base.connection.tables
182-
```
183-
184-
List all columns of a table
185-
```
186-
ActiveRecord::Base.connection.columns('student').map(&:name)
187-
```
188-
189-
When creating a new database, the following command tends to be faster
190-
191-
```
192-
bin/rails db:schema:load
193-
```
194-
195-
Delete all records in a database table
196-
197-
```
198-
Post.delete_all
199-
```
200-
201189

202190
# **Rails Best Practices**
203191

0 commit comments

Comments
 (0)