Databases & Rails: Week 2 Modeling Relationships And ActiveRecord
Last week we talked about why and how we can use databases to store information. This week we’re going to be focusing on how to use databases to model real world relationships and then use those relationships in Rails.
The first video in this series covers modeling a relationship between a car’s user and the car in databases. There are many different ways we could represent that relationship so this lecture covers quite a few of them, and talks about why or why not to represent your data using one of the methods.
The second video this week focuses on using ActiveRecord to build and create database backed Ruby objects (models) we’ll then create relationships between objects using our database.
First: Install Rails & Ruby on your machine and then complete the tutorial on building a Rails app. If you get stuck try googling for the answer, look for error messages, check your server logs, and you can always make a post on stack overflow. The point of this exercise isn’t to have a complete understanding of everything you’re doing, but to get you some hands on experience working with Rails from a high level. As we go on we’ll take a look at lower level functionality, and you will begin to feel more comfortable working with the technologies.
Second: On a piece of paper or in any computer program, write down the association between 5 different real world objects. Use the words
has_many through, and
has_and_belongs_to_many. If you want help on the associations you can refer to the rails association guide. Try explaining the relationships to someone who doesn’t program, if the relationships don’t make sense in english, they will make less sense when you map them to code :)
Thanks for stopping by if you feel like you’re lost in a video, go back and make sure you’ve seen the videos before it. As always if you have questions or comments message me @schneems