Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Knut Ringheim Lunde
Knut Ringheim Lunde
38,811 Points

Understanding Migration in Rails

In "Migrations and Relationsships" course in the video "Migrations" Hampton makes a Customer table with the fields: --> name, about, balance.

Later he makes an AddEmailAddress class where he adds a field to the Customer table. Why doesn't he just edit the Create Customers-file to include a email-field? I don't understand why he need to create a different file and class to do this? Or have I maybe completely misunderstood what he did in the first place?

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

In Rails the best practice is to introduce modifications to database schema in (mostly) reversible stages and these are called migrations. This way there is less risk that you will mess up something in your database and every step is reversible. It's similar to using Git - tracking every change to the database schema. You can then see how it all evolved over time. It's also helpful when working with other developers.

http://www.tutorialspoint.com/ruby-on-rails/rails-migrations.htm

http://guides.rubyonrails.org/migrations.html

http://archive.railsforum.com/viewtopic.php?id=1011

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

In general, any changes should be introduced using a new file that gets applied after the previous migration files. Once created, a migration file should not be modified.