You are previewing Learning Rails 3.

Learning Rails 3

Cover of Learning Rails 3 by Eric J Gruber... Published by O'Reilly Media, Inc.
  1. Learning Rails 3
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. Preface
    1. Who This Book Is For
    2. Who This Book Is Not For
    3. What You’ll Learn
    4. Ruby and Rails Style
    5. Other Options
    6. Rails Versions
    7. If You Have Problems Making Examples Work
    8. If You Like (or Don’t Like) This Book
    9. Conventions Used in This Book
    10. Using Code Examples
    11. Safari® Books Online
    12. How to Contact Us
    13. Acknowledgments
  4. 1. Starting Up Ruby on Rails
    1. If You Run Windows, You’re Lucky
    2. Getting Started at the Command Line
    3. Starting Up Rails
    4. Test Your Knowledge
      1. Quiz
      2. Answers
  5. 2. Rails on the Web
    1. Creating Your Own View
    2. What Are All Those Folders?
    3. Adding Some Data
    4. How Hello World Works
    5. Adding Logic to the View
    6. Test Your Knowledge
      1. Quiz
      2. Answers
  6. 3. Adding Web Style
    1. I Want My CSS!
    2. Specifying Stylesheets
    3. Creating a Layout for a Controller
    4. Choosing a Layout from a Controller
    5. Sharing Template Data with the Layout
    6. Setting a Default Page
    7. Test Your Knowledge
      1. Quiz
      2. Answers
  7. 4. Managing Data Flow: Controllers and Models
    1. Getting Started, Greeting Guests
    2. Application Flow
    3. Keeping Track: A Simple Guestbook
      1. Connecting to a Database Through a Model
      2. Connecting the Controller to the Model
    4. Finding Data with ActiveRecord
    5. Test Your Knowledge
      1. Quiz
      2. Answers
  8. 5. Accelerating Development with Scaffolding and REST
    1. A First Look at Scaffolding
    2. REST and Controller Best Practices
      1. Websites and Web Applications
      2. Toward a Cleaner Approach
    3. Examining a RESTful Controller
      1. Index: An Overview of Data
      2. Show: Just One Row of Data
      3. New: A Blank Set of Data Fields
      4. Edit: Hand Me That Data, Please
      5. Create: Save Something New
      6. Put This Updated Record In
      7. Destroy It
    4. Escaping the REST Prison
    5. Test Your Knowledge
      1. Quiz
      2. Answers
  9. 6. Presenting Models with Forms
    1. More Than a Name on a Form
    2. Generating HTML Forms with Scaffolding
    3. Form as a Wrapper
    4. Creating Text Fields and Text Areas
    5. Labels
    6. Creating Checkboxes
    7. Creating Radio Buttons
    8. Creating Selection Lists
    9. Dates and Times
    10. Creating Helper Methods
    11. Test Your Knowledge
      1. Quiz
      2. Answers
  10. 7. Strengthening Models with Validation
    1. Without Validation
    2. The Original Model
    3. The Power of Declarative Validation
    4. Managing Secrets
      1. Customizing the Message
      2. Limiting Choices
      3. Testing Format with Regular Expressions
      4. Seen It All Before
      5. Numbers Only
    5. A Place on the Calendar
      1. Testing for Presence
    6. Beyond Simple Declarations
      1. Test It Only If
      2. Do It Yourself
    7. Test Your Knowledge
      1. Quiz
      2. Answers
  11. 8. Improving Forms
    1. Adding a Picture by Uploading a File
      1. File Upload Forms
      2. Model and Migration Changes
      3. Results
    2. Standardizing Your Look with Form Builders
      1. Supporting Your Own Field Types
      2. Adding Automation
      3. Integrating Form Builders and Styles
    3. Test Your Knowledge
      1. Quiz
      2. Answers
  12. 9. Developing Model Relationships
    1. Connecting Awards to Students
      1. Establishing the Relationship
      2. Supporting the Relationship
      3. Guaranteeing a Relationship
    2. Connecting Students to Awards
      1. Removing Awards When Students Disappear
      2. Counting Awards for Students
    3. Nesting Awards in Students
      1. Changing the Routing
      2. Changing the Controller
      3. Changing the Award Views
      4. Connecting the Student Views
      5. Is Nesting Worth It?
    4. Many-to-Many: Connecting Students to Courses
      1. Creating Tables
      2. Connecting the Models
      3. Adding to the Controllers
      4. Adding Routing
      5. Supporting the Relationship Through Views
    5. What’s Missing?
    6. Test Your Knowledge
      1. Quiz
      2. Answers
  13. 10. Managing Databases with Migrations
    1. What Migrations Offer You
    2. Migration Basics
      1. Migration Files
      2. Running Migrations Forward and Backward
    3. Inside Migrations
      1. Working with Tables
      2. Data Types
      3. Working with Columns
      4. Indexes
      5. Other Opportunities
    4. Test Your Knowledge
      1. Quiz
      2. Answers
  14. 11. Debugging
    1. Creating Your Own Debugging Messages
    2. Raising Exceptions
    3. Logging
    4. Working with Rails from the Console
    5. The Ruby Debugger
    6. Test Your Knowledge
      1. Quiz
      2. Answers
  15. 12. Testing
    1. Test Mode
    2. Setting Up a Test Database with Fixtures
    3. Unit Testing
    4. Functional Testing
      1. Calling Controllers
      2. Testing Responses
      3. Dealing with Nested Resources
    5. Integration Testing
    6. Beyond the Basics
    7. Test Your Knowledge
      1. Quiz
      2. Answers
  16. 13. Sessions and Cookies
    1. Getting Into and Out of Cookies
    2. Storing Data Between Sessions
    3. Test Your Knowledge
      1. Quiz
      2. Answers
  17. 14. Users and Authentication
    1. Installation
    2. Storing Identities
    3. Storing User Data
    4. Wiring OmniAuth into the Application
    5. Classifying Users
    6. More Options
    7. Test Your Knowledge
      1. Quiz
      2. Answers
  18. 15. Routing
    1. Creating Routes to Interpret URIs
      1. Specifying Routes with match
      2. Globbing
      3. Regular Expressions and Routing
      4. A Domain Default with root
      5. Named Routes
      6. Mapping Resources
      7. Nesting Resources
      8. Route Order and Priority
      9. Checking the Map
    2. Generating URIs from Views and Controllers
      1. Pointing url_for in the Right Direction
      2. Adding Options
    3. Infinite Possibilities
    4. Test Your Knowledge
      1. Quiz
      2. Answers
  19. 16. From CSS to SASS
    1. Getting Started
    2. Sassy Style
      1. Variables
      2. Mixins
      3. Nesting
    3. Making Everything Work Together
      1. Becoming Sassier
    4. Test Your Knowledge
      1. Quiz
      2. Answers
  20. 17. Managing Assets and Bundles
    1. The Junk Drawer
      1. Sprockets
      2. Dissecting The Pipeline
      3. Putting It All Together
      4. Bundler
    2. Test Your Knowledge
      1. Quiz
      2. Answers
  21. 18. Sending Code to the Browser: JavaScript and CoffeeScript
    1. Sending JavaScript to the Browser
    2. Simplifying with CoffeeScript
      1. Have Some Sugar with your CoffeeScript
      2. Converting to CoffeeScript
    3. Test Your Knowledge
      1. Quiz
      2. Answers
  22. 19. Mail in Rails
    1. Sending Mail Messages
    2. Receiving Mail
      1. Setup
      2. Processing Messages
    3. Test Your Knowledge
      1. Quiz
      2. Answers
  23. 20. Pushing Further into Rails
    1. Changing to Production Mode
    2. Deploying Is Much More Than Programming
    3. Joining the Rails Ecosystem
      1. Keep Up with Rails
      2. Ruby
      3. Working With and Around Rails
      4. Keep Exploring
  24. A. An Incredibly Brief Introduction to Ruby
    1. How Ruby Works
    2. How Rails Works
    3. Getting Started with Classes and Objects
    4. Comments
    5. Variables, Methods, and Attributes
      1. Variables
      2. Methods
      3. Attributes
    6. Logic and Conditionals
      1. Operators
      2. if, else, unless, and elsif
      3. ?:
      4. case and when
      5. Loops
      6. Many More Possibilities
  25. B. An Incredibly Brief Introduction to Relational Databases
    1. Tables of Data
      1. Connecting Tables
      2. Using Tables to Connect Tables
      3. Granularity
    2. Databases, Tables, and Rails
  26. C. An Incredibly Brief Guide to Regular Expressions
    1. What Regular Expressions Do
    2. Starting Small
    3. The Simplest Expressions: Literal Strings
    4. Character Classes
    5. Escaping
    6. Modifiers
    7. Anchors
    8. Sequences, Repetition, Groups, and Choices
    9. Greed
    10. More Possibilities
  27. D. Glossary
    1. Speaking in Rails
  28. Index
  29. About the Authors
  30. Colophon
  31. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  32. Copyright
O'Reilly logo

Chapter 10. Managing Databases with Migrations

Migrations might seem strange at first, but over time they’ll become a very ordinary part of your work, whether you generate them automatically or customize them by hand. Rails’ approach to managing data structures is very different from the traditional separation of database design from programming. While Rails still maintains a separate toolkit for defining data structures, that toolkit attempts to improve on the traditional SQL Data Definition Language (DDL) by wrapping DDL in Ruby code.

Migrations are something of a world of their own in the Rails environment, but they are still recognizably Rails, built into the same development process. Migrations are all written in Ruby code, using a fairly small set of conventions. This book has used migrations throughout—you can write much of a Rails application without them—but until the last chapter (and then only once), those migrations were generated using Rails’ inventive scripts. Once you move past those scripts, migrations are a little more difficult, but still not that complicated.

Note

The details of migrations may not be your first priority. You can safely skip this chapter and come back to it if database and data structure management seem like good reading for a really rainy day.

What Migrations Offer You

Migrations are part of Rails’ general effort to separate developers from direct contact with databases. From a Rails perspective, databases are kind of a “giant hash in the sky,” a conveniently ...

The best content for your career. Discover unlimited learning on demand for around $1/day.