Migration naming conventions

I notice that when I generate a migration alone using the migration
generator, the name of the resulting file is something like “db/
migrate/001_members.rb” and that file contains a class named
“Members”. However, if instead I generate a model and let that
process create the migration for me, the name of the resulting file is
something like “db/migrate/001_create_members.rb” and that file
contains a class named “CreateMembers”. Information that I’ve been
able to find on the web (e.g.,
http://www.railsforum.com/viewtopic.php?id=1011)
seems to imply (although it doesn’t specifically say) that the actual
base names of the file doesn’t really matter, it’s only the pre-
appended number that’s important. I would think that the base name of
the file would have to identify the table; otherwise, how would rake
know what table is being modified in subsequent migrations? Anyway, I
know that I must be missing something. Can someone please clarify the
confusion for me?

Thanks for any input.

      ... doug

The class name of the migration must match the file name (without the
leading number). The leading number is used for the order (try changing
the name of a migration and see what happens).

Fred

doug wrote:

I notice that when I generate a migration alone using the migration
generator, the name of the resulting file is something like “db/
migrate/001_members.rb” and that file contains a class named
“Members”. However, if instead I generate a model and let that
process create the migration for me, the name of the resulting file is
something like “db/migrate/001_create_members.rb” and that file
contains a class named “CreateMembers”.

When you create a model (maybe with attributes) the generator figures
there will be a table behind the model that needs to be created. So it
generates some ruby code in a class it names Create and a
file named “xxx_create_”. This convention allows Rails to
know what migrations are associated with what Models (and other things).

When you want just a migration, the generator doesn’t know what your
migration is going (perhaps you’re adding a column? Data? Changing
something?). If you were too add columns, the convention is to uses a
name like add_<column_names>, but that’s just for you to help remember.
The generator uses that name to come up with a class name for the
migration, a file name, and then uses the data in the table schema_info
to come up with the next sequence number. The rest is up to you.

Said another way, migrations don’t use naming conventions in order to
execute, they just use the serial number to know which version of the
schema to create, and in what order.

I get it!

Thanks, guys.

   ... doug

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs