im trying to understand the process of creating tables in ruby-on-rails 3.
i have read about migrations. so i am supposed to create tables by editing in the files in:
Database Migrations/migrate/20100611214419_create_posts
Database Migrations/migrate/20100611214419_create_categories
but they were generated by:
rails generate model Post name:string description:text
rails generate model Category name:string description:text
does this mean i have to use “rails generate model” command everytime i want to create a table?
what if i create a migration file but want to add columns. do i create another migration file for adding those or do i edit the existing migration file directly? the guide told me to add a new one, but here is the part i dont understand. why would i add a new one? cause then the new state will be dependent of 2 migration files.
and how do i add a new migration file for updating then? what is the command? and if i have to drop columns or edit them. how do it do that?
rails generate model Post name:string description:text
cause the above command just add columns.
and if i don’t use the commands, how do i create migration files?
in symfony i just edit a schema.yml file directly, there are no migration files with versioning and so on.
and i think in django you just create the models and it will create the database tables.
im new to RoR and want to get the picture of creating tables.
thanks
If you want to update a table you have to create a new migration file because each migrations is executed only once on the database. So if you already have a
poststable then after modifying thecreate_postsmigration you won’t be able to run it again.You can rollback migrations and then run them again. That would solve the problem but it would also destroy the table and the data that it might hold. This isn’t a problem if you just created the migration and then noticed that you missed one column. Then you can just add the column to the migration, rollback and migrate. But you don’t want to do that on a production database!
To create a new migration you just run:
If you call your migration
add_*_to_tablethen you can also pass the same arguments as ingenerate model:This will automatically generate this migration:
This will work with
remove_*_from_tabletoo:The migration will be:
Here are some more methods that you can use in your migrations.