Is there a best practice naming convention for Rails migrations, particularly when editing a model?
e.g. if I'm adding a column bar
to the Foo
model, should I name it edit_foo
or add_bar_to_foo
I'm assuming if I'm editing mutliple models then I should create multiple migrations, but what if I'm making multiple modifications to a single model, do I name it add_bar_remove_x_edit_y_to_foo
?
Naming conventions in Active Record model Rails is capable of pluralizing (and singularizing) both regular and irregular words. Model class names must use the CamelCase form when composed of two or more words, while the database table names must use the snake_case form.
By convention, method names begin with a lowercase letter. (Method names can begin with a capital letter, but that makes them look like constants.) When a method name is longer than one word, the usual convention is to separate the words with underscores like_this rather than using mixed case likeThis .
Rails creates a table in your database called schema_migrations to keep track of which migrations have run. The table contains a single column, version . When Rails runs a migration, it takes the leading digits in the migration's file name and inserts a row for that "version", indicating it has been run.
What Is a Naming Convention? In simple terms, a naming convention refers to a framework used for naming your files in a specific way. This should be descriptive and consistent throughout the organization. It is always best to use a naming convention to describe the contents of the files.
I agree with the previous poster. The naming should focus on readability. But also keep in mind that you can't (nor should) have two migrations with the same name.
So, general names like edit_foo_model
is generally not a good idea (since, what happens when you want to add more columns to that model), then it would be better to group the columns into what the purpose is, like update_foo_for_bar_support
. You can usually skip adding model since, well, everyone knows that migrations do handle models, so there is no need to mention that in the name (that is, update_foo
instead of update_foo_model
).
Also, what I usually do is to keep different changes separated. So, if there are multiple different changes in a model, I'd separate them into different migration files, one for adding columns and one for removing columns for instance.
I would split up multiple schema changes into multiple migrations so that you can easily name the single migrations!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With