Appendix B. An Incredibly Brief Introduction to Relational Databases

It’s impossible to move, to live, to operate at any level without leaving traces, bits, seemingly meaningless fragments of personal information.

William Gibson

“I thought the whole point of Rails was that it hid the database and just let me write Ruby code! Why do I need to know about these things?”

Rails has all kinds of features for building web applications, but its foundation component is the way that it lets you get information into and out of relational databases. You can build simple applications without knowing much about databases, just telling Rake to do a few things and making sure you gave Rails the right data type for each field. You don’t need to know Structured Query Language (SQL), the classic language for working with databases.

Building a more complex Rails application, though, really demands at least a basic understanding of how relational databases work. It helps to think about tables and their connections when defining Rails models, at least when you first set them up.

Note

NoSQL databases are an alternative approach in Rails. Tools like Firebase, CouchDB, MongoDB, and many others have gems available that make them easy to plug into your Rails app. However, traditional SQL databases are still at the heart of Rails, so we will use those examples in this appendix.

Tables of Data

The foundational idea behind relational databases is a simple but powerful structure. Each table is a ...

Get Learning Rails 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.