Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
82: Active Record Associations
Think about how you organize a professional kitchen. You have a Head Chef who manages a team of Sous Chefs. From the Head Chef's perspective, they are looking at a group of people they're responsible for. But from a Sous Chef's perspective, they have exactly one boss they report to. Now, consider the ingredients. A specific recipe uses many ingredients, and a single ingredient—like salt—is used in almost every recipe in the building. That's a completely different kind of relationship.
In Active Record, associations are just a way of telling Ruby how these database tables relate to one another so you don't have to write manual SQL joins every time you want to find out who a task belongs to. I've spent years writing these, and the secret is realizing that the "relationship" is actually just a fancy wrapper around a foreign key (an ID column) in your database.
Linking the One-to-Many Chain
The most common relationship is the one-to-many. Let's use a project management app as our example. A Project has many Tasks, and each Task belongs to one Project.
class Project < ApplicationRecord
has_many :tasks
end
class Task < ApplicationRecord
belongs_to :project
end
Here is the part that trips people up: the belongs_to side is the one that actually holds the data. In your database, the tasks table must have a project_id column. I like to remember it this way: whoever "belongs to" the other is the one carrying the ID badge of their owner. Once you've set this up, you stop thinking about IDs and start thinking about objects. Instead of searching for tasks where project_id == 5, you just call my_project.tasks.
The Singular Focus of has_one
Sometimes you have a one-to-one relationship. Maybe a User has exactly one AccountSettings record. It looks almost identical to has_many, but it returns a single object instead of a collection.
class User < ApplicationRecord
has_one :account_setting
end
class AccountSetting < ApplicationRecord
belongs_to :user
end
Just like before, the AccountSetting table is the one that holds the user_id. If you find yourself using has_one and belongs_to on both sides, stop. You're overcomplicating it. Only one side can "belong" (hold the foreign key).
Managing the Many-to-Many Web
This is where things get interesting. What if a User can be assigned to many Projects, and a Project can have many Users? You can't put a user_id on the projects table, because there are too many users. You can't put a project_id on the users table, because they're in too many projects.
We solve this with a "join model." Think of it as a sign-up sheet. The ProjectMembership model exists solely to link a user to a project.
class User < ApplicationRecord
has_many :project_memberships
has_many :projects, through: :project_memberships
end
class ProjectMembership < ApplicationRecord
belongs_to :user
belongs_to :project
end
class Project < ApplicationRecord
has_many :project_memberships
has_many :users, through: :project_memberships
end
The through: :project_memberships part is the magic. It tells Active Record: "If I ask for a user's projects, first go look at the project_memberships table, find all the entries for this user, and then go grab the projects associated with those entries." It's a two-step jump, but Ruby handles it in one method call. I highly recommend using has_many :through over has_and_belongs_to_many because the join model allows you to store extra data—like the date the user joined the project—right on the relationship itself.
📋 Practical Task
Building a Course Enrollment System
You need to implement the backend associations for a learning platform. Your goal is to set up the models so that a student can be enrolled in multiple courses, and a course can have multiple students.
Requirements:
- Create a
Coursemodel that has manyEnrollmentsand manyStudentsthrough those enrollments. - Create a
Studentmodel that has manyEnrollmentsand manyCoursesthrough those enrollments. - Create an
Enrollmentmodel that belongs to both aStudentand aCourse. - Ensure the
Coursemodel also has a one-to-one relationship with aCourseSyllabusmodel (where the syllabus belongs to the course).
Write the Ruby code for these four classes. Make sure your associations are placed in the correct models and that you've correctly identified which model "belongs to" the others.
There are no comments for now.