DEV Community

aalcantara1
aalcantara1

Posted on

03/28/2026

General process after rails generate scaffold and before rails db:migrate
In Rails, the scaffold command is just the starting point. The generator gives you a rough draft, and your job is to review and customize it before it touches the database.

Here is the usual process:

Generate the scaffold
rails generate scaffold Comment author:references photo:references body:text
Look at what Rails created A scaffold usually creates:
a migration
a model
a controller
views
routes
tests/spec files
Edit the migration first This is the most important step before migrating. Review the generated migration and fix anything the generator could not know about your app’s business rules.

Common things to change:

add null: false where blank values should not be allowed
add default: values if needed
fix foreign keys when the association name does not match the table name
make sure column types are correct
add indexes or uniqueness constraints if needed
Example:

t.references :author, null: false, foreign_key: { to_table: :users }
t.references :photo, null: false, foreign_key: true
t.text :body, null: false

Why this matters:

the scaffold only guesses
it does not know your custom association names
it does not know which fields should be required
Edit the model After the migration looks right, update the model so Rails understands the relationships and rules.

Common things to add:

belongs_to
has_many
class_name:
foreign_key:
validates
enum
scopes
counter_cache: true
Example:

class Comment < ApplicationRecord
belongs_to :author, class_name: "User", counter_cache: true
belongs_to :photo, counter_cache: true

validates :body, presence: true

scope :default_order, -> { order(created_at: :asc) }
end
If needed, edit related models too If you added a new belongs_to, you often also need the matching has_many on the other model.

Example:

class User < ApplicationRecord
has_many :comments, foreign_key: :author_id, dependent: :destroy
end
And:

class Photo < ApplicationRecord
has_many :comments, dependent: :destroy
end
Review for naming mismatches This is a very common source of bugs.

If you use a custom association name like:

author instead of user
fan instead of user
owner instead of user
then you usually need:

foreign_key: { to_table: :users } in the migration
class_name: "User" in the model
Optionally commit the generated files before editing This is not required, but it is a very good habit.

Example:

git add -A
git commit -m "Generated Comments scaffold"
Then after your edits:

git add -A
git commit -m "Configured Comment migration and model"
This makes it easier to separate:

what Rails generated
what you changed
Then migrate
rails db:migrate
Short version
A good rule of thumb is:

generate scaffold
inspect generated files
fix the migration
fix the model
add/update related associations
then migrate
What you should especially check before migrating
Before you run rails db:migrate, ask yourself:

Are the foreign keys pointing to the correct tables?
Should any columns have null: false?
Should any columns have a default value?
Do I need class_name: because I used a custom association name?
Do I need validations in the model to match the database rules?
Do I need matching has_many associations in other models?
In one sentence
The general process is: use scaffold to generate the rough draft, then manually review and customize the migration and models before running the migration.

Top comments (0)