0
0
Ruby on Railsframework~10 mins

Why associations connect models in Ruby on Rails - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why associations connect models
Define Model A
Define Model B
Add association in Model A
Add association in Model B
Rails links models
Use association methods
Access related records easily
Associations in Rails connect two models by defining relationships in each model, enabling easy access to related data.
Execution Sample
Ruby on Rails
class Author < ApplicationRecord
  has_many :books
end

class Book < ApplicationRecord
  belongs_to :author
end
Defines two models, Author and Book, where an author can have many books and each book belongs to one author.
Execution Table
StepActionModel StateAssociation Method CreatedResult
1Define Author modelAuthor model existsNone yetReady for associations
2Define Book modelBook model existsNone yetReady for associations
3Add has_many :books to AuthorAuthor knows it has many booksauthor.booksCan call author.books to get books
4Add belongs_to :author to BookBook knows it belongs to authorbook.authorCan call book.author to get author
5Create author instanceauthor object createdauthor.booksEmpty collection initially
6Create book instance linked to authorbook object created with author_idbook.authorReturns linked author
7Call author.booksauthor.books returns collectionauthor.booksIncludes created book
8Call book.authorbook.author returns authorbook.authorReturns author instance
9ExitAssociations set up and workingauthor.books, book.authorModels connected via associations
💡 Associations connect models allowing easy navigation between related records
Variable Tracker
VariableStartAfter Step 5After Step 6After Step 7Final
authornilAuthor instanceAuthor instanceAuthor instanceAuthor instance
booknilnilBook instance linked to authorBook instance linked to authorBook instance linked to author
author.booksnilempty collectionempty collectioncollection with bookcollection with book
book.authornilnilauthor instanceauthor instanceauthor instance
Key Moments - 3 Insights
Why do we add has_many in one model and belongs_to in the other?
Because has_many sets up a collection on one side, and belongs_to sets up a single link on the other, connecting the two models as shown in steps 3 and 4.
How does Rails know which records belong together?
Rails uses foreign keys like author_id in the Book model to link records, as seen in step 6 where book is created with author_id.
What happens if we call author.books before adding any books?
It returns an empty collection, as shown in step 5, meaning no books are linked yet.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does author.books return at step 7?
AAn empty collection
BA collection including the created book
CNil
DAn error
💡 Hint
Check the 'Result' column at step 7 in the execution_table
At which step does the book instance get linked to the author?
AStep 6
BStep 5
CStep 3
DStep 8
💡 Hint
Look at the 'Model State' and 'Result' columns for when book has author_id
If we remove belongs_to :author from Book, what changes in the execution?
Aauthor.books still works normally
Bauthor.books returns nil
Cbook.author method will not exist
DNo change at all
💡 Hint
Refer to step 4 where belongs_to creates book.author method
Concept Snapshot
Rails associations connect models by defining relationships.
Use has_many on one model and belongs_to on the other.
This creates methods to access related records easily.
Foreign keys link records behind the scenes.
Associations simplify data navigation in Rails apps.
Full Transcript
In Rails, associations connect models by defining relationships between them. For example, an Author model can have many Book models, and each Book belongs to one Author. We add has_many :books in the Author model and belongs_to :author in the Book model. This setup creates methods like author.books to get all books for an author, and book.author to get the author of a book. Behind the scenes, Rails uses foreign keys like author_id in the Book model to link records. When we create an author and then a book linked to that author, calling author.books returns the collection including that book, and book.author returns the author instance. This connection makes it easy to navigate related data in Rails applications.