title that can hold a string which for most databases means up to 255

Title that can hold a string which for most databases

This preview shows page 141 - 143 out of 517 pages.

title that can hold a string, which for most databases means up to 255 characters. 1 class CreateMovies < ActiveRecord::Migration 2 def up 3 create_table ’movies’ do |t| 4 t.string ’title’ 5 t.string ’rating’ 6 t.text ’description’ 7 t.datetime ’release_date’ 8 # Add fields that let Rails automatically keep track 9 # of when movies are added or modified: 10 t.timestamps 11 end 12 end 13 14 def down 15 drop_table ’movies’ # deletes the whole table and all its data! 16 end 17 end Figure 4.3: A migration that creates a new Movies table, specifying the desired fields and their types. The documentation for the ActiveRecord::Migration class (from which all migrations inherit) is part of the Rails documentation , and gives more details and other migration options. Save the file and type rake db:migrate to actually apply the migration and create this table. Note that this housekeeping task also stores the migration number itself in the database, and by default it only applies migrations that haven’t already been applied. (Type rake db:migrate again to verify that it does nothing the second time.) rake db:rollback will “undo” the last migration by running its down method. (Try it. And then run rake db:migrate to re-apply the migration.) However, some migrations, such as those that delete data, can’t be “undone”; in these cases, the down method should raise an ActiveRecord::IrreversibleMigration exception. Summary Rails defines three environments—development, production and test—each with its own copy of the database. A migration is a script describing a specific set of changes to the database. As apps evolve and add features, migrations are added to express the database changes required to support those new
Image of page 141
features. Changing a database using a migration takes three steps: create the migration, apply the migration to your development database, and (if applicable) after testing your code apply the migration to your production database. The rails generate migration generator fills in the boilerplate for a new migration, and the ActiveRecord::Migration class contains helpful methods for defining it. rake db:migrate applies only those migrations not already applied to the development database. The method for applying migrations to a production database depends on the deployment environment. ELABORATION: Environments Different environments can also override specific app behaviors. For example, production mode might specify optimizations that give better performance but complicate debugging if used in development mode. Test mode may “stub out” external interactions, for example, saving outgoing emails to a file rather than actually sending them. The file config/environment.rb specifies general startup instructions for the app, but config/environments/production.rb allows setting specific options used only in production mode, and similarly development.rb and test.rb in the same directory.
Image of page 142
Image of page 143

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors