Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming conventions for Rails migrations

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?

like image 456
roryf Avatar asked Sep 16 '09 08:09

roryf


People also ask

What are the rails naming conventions?

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.

What are the conventions for naming methods in Ruby?

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 .

How does rails know which migrations to run?

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 are naming conventions?

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.


2 Answers

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.

like image 149
Jimmy Stenke Avatar answered Oct 10 '22 08:10

Jimmy Stenke


I would split up multiple schema changes into multiple migrations so that you can easily name the single migrations!

like image 28
BvuRVKyUVlViVIc7 Avatar answered Oct 10 '22 06:10

BvuRVKyUVlViVIc7