Chapter 9. Developing Model Relationships
Much of what I do in my job is think about whether relationships we see in data are causal, as opposed to just reflecting correlations.
Emily Oster
Everything you’ve done so far has been in the context of an application with one and only one table. That’s actually enough power to run a lot of different projects from contact managers to time-series data collection. However, you’ll quickly find that most of the projects for which it’s worth creating a web application require more than just one table. Fortunately, Rails makes that easy, giving you the tools you need to create multiple tables and manage even complex relationships between them.
Note
If you don’t know much about databases, now is a good time to visit Appendix B. Up to this point, it’s been possible to largely forget that there was a relational database underneath the application except for some mechanics. From this point on, you’ll need to understand how tables work in order to understand how Rails models work. (You still don’t need to understand SQL, however.)
Working with multiple tables is, on the surface, pretty simple. Every Rails model maps to a table, so working with multiple tables just means working with multiple models. The hard part is managing the relationships between the tables—which in Rails demands managing the relationships between models.
Most of the steps for working with multiple models are the same as for working with single models, just done once for ...
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.