ActiveRecord Associations
Published on 27 Mar 2020
This is a simple explanation of some basic concepts of Active Record Assocations.
School Example
We have the following entities:
- Classroom
- Teacher
- Pupil
- Subject
Active Record Assocations
Classroom has_one :teacher
Teacher belongs_to :classroom
Teacher has_many :courses
Course belongs_to :teachers
Course has_and_belongs_to_many :pupils
Pupil has_and_belongs_to_many :courses
Database Tables
classrooms |
---|
id |
room_number |
teachers | foreign_key |
---|---|
id | |
name | |
classroom_id | classrooms.id |
courses | foreign_key |
---|---|
id | |
title | |
teacher_id | teachers.id |
pupils |
---|
id |
name |
course_pupils | foreign_key |
---|---|
pupil_id | pupils.id |
course_id | courses.id |
all tags
activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career cli cloud code coding config configuration cp crud css database db design devops django email erp filter fugitive gif gist git gnome grep hebrew http ide isbn-fetcher iso javascript job search js kanban kanban\ kindle koans linux logger manjaro map markdown microservices mobi mtp neovim nodejs packages pastbin patch post python rails reduce refactoring rest routes rspec ruby scripting security sed shell sql string_replacement study tdd terminal testing version_control vim walkthrough workflow