Models relationships — Ruby Active Record

The Why

Before Active Record, if you wanted to access something in another table, you would need to filter through their foreign key column and compare the values to your own id, until one matches, and then you would need to store this information in a method or a variable in your code.

Generating a Model

Has_many, Belongs_to, and Has_one

Accessing the other tables

Now with our relationships set, if for example, we want to know what’s the first article our writer wrote, we only need to type:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Renata Miriuk

Renata Miriuk

Interested in everything, writing about tech