0
0
Ruby on Railsframework~10 mins

Many-to-many with has_many through in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Many-to-many with has_many through
Define Models
Create Join Model
Set has_many through
Create Records
Access Associations
Use join model for extra data
This flow shows how Rails sets up two models connected by a join model, allowing many-to-many links with extra data.
Execution Sample
Ruby on Rails
class Author < ApplicationRecord
  has_many :authorships
  has_many :books, through: :authorships
end
Defines Author model linked to Books through Authorship join model.
Execution Table
StepActionModel StateAssociation StateOutput/Result
1Define Author, Book, Authorship modelsModels definedNo records yetReady for associations
2Author has_many :authorshipsAuthor model knows AuthorshipNo recordsAssociation set
3Author has_many :books through :authorshipsAuthor model knows Books via AuthorshipNo recordsAssociation set
4Create Author record (id=1)Author(id=1)No associations yetAuthor saved
5Create Book record (id=10)Book(id=10)No associations yetBook saved
6Create Authorship record (author_id=1, book_id=10)Authorship links Author 1 and Book 10Author 1 linked to Book 10Association created
7Access author.booksAuthor(id=1)Books through AuthorshipReturns [Book(id=10)]
8Access book.authorsBook(id=10)Authors through AuthorshipReturns [Author(id=1)]
9Add extra data to Authorship (role: 'co-author')Authorship record updatedAssociation with extra infoExtra data stored
10Access authorship.roleAuthorship recordRole = 'co-author'Returns 'co-author'
11End of flowAll models and associations setAssociations accessibleMany-to-many works with extra data
💡 All associations created and accessible; many-to-many with extra data works
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 9Final
Author records[][Author(id=1)][Author(id=1)][Author(id=1)][Author(id=1)][Author(id=1)]
Book records[][][Book(id=10)][Book(id=10)][Book(id=10)][Book(id=10)]
Authorship records[][][][Authorship(author_id=1, book_id=10)][Authorship(author_id=1, book_id=10, role='co-author')][Authorship(author_id=1, book_id=10, role='co-author')]
author.books[][][][Book(id=10)][Book(id=10)][Book(id=10)]
book.authors[][][][Author(id=1)][Author(id=1)][Author(id=1)]
Key Moments - 3 Insights
Why do we need a join model like Authorship instead of just has_and_belongs_to_many?
Because has_many through allows storing extra data (like role) on the join model, which has_and_belongs_to_many does not support. See step 9 where role is added.
How does author.books know which books belong to the author?
author.books uses the Authorship join records to find books linked to that author. See step 7 where author.books returns Book(id=10) via Authorship.
Can we access extra data on the join model from the author or book?
No, you must access the join model directly (Authorship) to get extra data like role. See step 10 where authorship.role returns 'co-author'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does author.books return at step 7?
A[Author(id=1)]
B[]
C[Book(id=10)]
Dnil
💡 Hint
Check the 'Output/Result' column at step 7 in the execution_table.
At which step is extra data added to the join model?
AStep 6
BStep 9
CStep 4
DStep 10
💡 Hint
Look for when 'role: co-author' is added in the execution_table.
If we remove the has_many :authorships association from Author, what happens to author.books?
Aauthor.books raises an error
Bauthor.books still works normally
Cauthor.books returns empty array
Dauthor.books returns all books
💡 Hint
Think about how author.books depends on has_many :authorships in the model definition.
Concept Snapshot
Many-to-many with has_many through:
- Use a join model (e.g., Authorship) between two models (Author, Book)
- Define has_many :join_model and has_many :other_model, through: :join_model
- Allows storing extra data on join model
- Access associations via model.join_model or model.other_model
- Enables flexible, rich many-to-many relationships
Full Transcript
This visual execution trace shows how Rails sets up a many-to-many relationship using has_many through. First, models Author, Book, and the join model Authorship are defined. Author has many authorships and many books through authorships. Records are created for author and book, then an authorship record links them. Accessing author.books returns the linked book, and book.authors returns the linked author. Extra data like role can be stored on the authorship join model. The trace highlights key steps and variable states, clarifying how associations work and how extra data is handled on the join model.