@@ -250,71 +250,71 @@ purposes.
250
250
Active Record models are placed under the ` app/models ` directory by default. But
251
251
you may want to organize your models by placing similar models under their own
252
252
folder and namespace. For example, ` order.rb ` and ` review.rb ` under
253
- ` app/models/product ` with ` Product ::Order` and ` Product ::Review` class names,
253
+ ` app/models/books ` with ` Book ::Order` and ` Book ::Review` class names,
254
254
respectively. You can create namespaced models with Active Record.
255
255
256
- In the case where the ` Product ` module does not already exist, the ` generate `
256
+ In the case where the ` Book ` module does not already exist, the ` generate `
257
257
command will create everything like this:
258
258
259
259
``` bash
260
- $ bin/rails generate model Product ::Order
260
+ $ bin/rails generate model Book ::Order
261
261
invoke active_record
262
- create db/migrate/20240306194227_create_product_orders .rb
263
- create app/models/product /order.rb
264
- create app/models/product .rb
262
+ create db/migrate/20240306194227_create_book_orders .rb
263
+ create app/models/book /order.rb
264
+ create app/models/book .rb
265
265
invoke test_unit
266
- create test/models/product /order_test.rb
267
- create test/fixtures/product /orders.yml
266
+ create test/models/book /order_test.rb
267
+ create test/fixtures/book /orders.yml
268
268
```
269
269
270
- If the ` Product ` module already exists, you will be asked to resolve
270
+ If the ` Book ` module already exists, you will be asked to resolve
271
271
the conflict:
272
272
273
273
``` bash
274
- $ bin/rails generate model Product ::Order
274
+ $ bin/rails generate model Book ::Order
275
275
invoke active_record
276
- create db/migrate/20240305140356_create_product_orders .rb
277
- create app/models/product /order.rb
278
- conflict app/models/product .rb
279
- Overwrite /Users/bhumi/Code/rails_guides/app/models/product .rb? (enter " h" for help) [Ynaqdhm]
276
+ create db/migrate/20240305140356_create_book_orders .rb
277
+ create app/models/book /order.rb
278
+ conflict app/models/book .rb
279
+ Overwrite /Users/bhumi/Code/rails_guides/app/models/book .rb? (enter " h" for help) [Ynaqdhm]
280
280
```
281
281
282
- Once the namespaced model generation is successful, the ` Product ` and ` Order `
282
+ Once the namespaced model generation is successful, the ` Book ` and ` Order `
283
283
classes look like this:
284
284
285
285
``` ruby
286
- # app/models/product .rb
287
- module Product
286
+ # app/models/book .rb
287
+ module Book
288
288
def self .table_name_prefix
289
- " product_ "
289
+ " book_ "
290
290
end
291
291
end
292
292
293
- # app/models/product /order.rb
294
- class Product ::Order < ApplicationRecord
293
+ # app/models/book /order.rb
294
+ class Book ::Order < ApplicationRecord
295
295
end
296
296
```
297
297
298
298
Setting the
299
299
[ table_name_prefix] ( https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema.html#method-c-table_name_prefix-3D )
300
- in ` Product ` will allow ` Order ` model's database table to be named
301
- ` product_orders ` , instead of plain ` orders ` .
300
+ in ` Book ` will allow ` Order ` model's database table to be named
301
+ ` book_orders ` , instead of plain ` orders ` .
302
302
303
- The other possibility is that you already have a ` Product ` model that you want
303
+ The other possibility is that you already have a ` Book ` model that you want
304
304
to keep in ` app/models ` . In that case, you can choose ` n ` to not overwrite
305
- ` product .rb` during the ` generate ` command.
305
+ ` book .rb` during the ` generate ` command.
306
306
307
- This will still allow for a namespaced table name for ` Product ::Order` class,
307
+ This will still allow for a namespaced table name for ` Book ::Order` class,
308
308
without needing the ` table_name_prefix ` :
309
309
310
310
``` ruby
311
- # app/models/product .rb
312
- class Product < ApplicationRecord
311
+ # app/models/book .rb
312
+ class Book < ApplicationRecord
313
313
# existing code
314
314
end
315
315
316
- Product ::Order .table_name
317
- # => "product_orders "
316
+ Book ::Order .table_name
317
+ # => "book_orders "
318
318
```
319
319
320
320
Overriding the Naming Conventions
0 commit comments