问题
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
?
回答1:
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.
回答2:
I would split up multiple schema changes in multiple migrations! Then you can easyly name the single migrations!
回答3:
The point is readability- to find out fast what the migration is responsible for.. if you write too much "data" in the name, it makes it harder to scan and you shoot yourself in the foot.
So.. if it's 1-2 changes, write it in the name, if there are too many changes, write update_foo_model
(or edit_foo_model)
来源:https://stackoverflow.com/questions/1431658/naming-conventions-for-rails-migrations