When generating a scaffold
in Rails it automatically creates the routes and pluralized names for me, example:
rails g scaffold Trabajador nombre apellidos cargo
And Rails generates it as trabajadors
.
My questions are:
- Is there any way to define the name I want when generating the
scaffold
?, any way to specify what I want it to be calledtrabajadores
and nottrabajadors
? - Can it be changed once generated since I have created some this way?
Update
You can specify rails how to pluralize a specific word by modifying the file
infelctions.rb
:After making that change, you can run your normal command
scaffold
and the plural oftrabajador
will betrabajadores
app-wide.Yes, you make the same change described in the previous point (modify
inflections.rb
) and manually make the following changes (for example, to changetrabajadors
):Change
resources :trabajadors
toresources :trabajadores
inroutes.rb
:Rename your controller from
trabajadors
totrabajadores
(including the file name):Rename the table for the model
Trabajador
:Create the migration:
Open the generated file and modify it to look like this:
Run the migration:
original answer
You can't do it directly in the command
scaffold
, but you can manually modify the name once you've run the command.For example, first run
rails g scaffold
(unlike your example, I'm specifying the data type on each column):Then modify your migration so that the file name and table name are
trabajadores
instead oftrabajadors
, and change the class name to beCreateTrabajadores
instead ofCreateTrabajadors
1 :Run the migration:
Now, in your model, add the table name:
This step is essential so that your model does not look for the table
trabajadors
, which is the rails standard.1 * Strictly you could just change the name of the table and that would work fine, however I recommend changing all the names to keep a better order.
Yes, following the steps described above; but consider that, if you already ran the migration (and what's more, your table has data), you will have to change the name of the table without deleting it; You can achieve this with another migration, for example:
Open the generated file and modify it to look like this:
And run the migration: