Challenge - 5 Problems
Master of Many-to-many Associations
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Understanding the association output
Given the following Rails models, what will
user.posts return after creating the associations?class User < ApplicationRecord has_many :user_posts has_many :posts, through: :user_posts end class Post < ApplicationRecord has_many :user_posts has_many :users, through: :user_posts end class UserPost < ApplicationRecord belongs_to :user belongs_to :post end # Setup user = User.create(name: 'Alice') post1 = Post.create(title: 'Hello') post2 = Post.create(title: 'World') UserPost.create(user: user, post: post1) UserPost.create(user: user, post: post2)
Attempts:
2 left
💡 Hint
Think about how has_many through works to connect user and posts via user_posts.
✗ Incorrect
The user has two associated posts through the user_posts join table, so user.posts returns both post1 and post2 objects.
📝 Syntax
intermediate2:00remaining
Correct syntax for has_many through association
Which of the following is the correct way to declare a many-to-many association between
Author and Book using a join model Authorship in Rails?Attempts:
2 left
💡 Hint
Remember the join model should be declared first, then the through association.
✗ Incorrect
Option B correctly declares the join model association first, then the has_many through association. Other options have incorrect order or missing associations.
🔧 Debug
advanced2:00remaining
Why does user.posts return empty?
Given these models:
Why does
class User < ApplicationRecord has_many :user_posts has_many :posts, through: :user_posts end class Post < ApplicationRecord has_many :user_posts has_many :users, through: :user_posts end class UserPost < ApplicationRecord belongs_to :user belongs_to :post end # Data user = User.create(name: 'Bob') post = Post.create(title: 'Rails Tips') # Missing association creation here puts user.posts.inspect
Why does
user.posts return an empty array?Attempts:
2 left
💡 Hint
Check if the join table has any records connecting user and post.
✗ Incorrect
The join table UserPost must have a record linking the user and post. Without it, user.posts returns empty.
❓ state_output
advanced2:00remaining
Effect of destroying a join record
If a
UserPost record linking a user and a post is destroyed, what happens to user.posts?Attempts:
2 left
💡 Hint
Think about what destroying a join record affects in a many-to-many association.
✗ Incorrect
Destroying the join record removes the link, so user.posts no longer includes that post, but the post itself still exists.
🧠 Conceptual
expert2:00remaining
Why use has_many through instead of has_and_belongs_to_many?
Which is the main advantage of using
has_many :through over has_and_belongs_to_many in Rails many-to-many associations?Attempts:
2 left
💡 Hint
Think about what you can do with the join model in has_many through.
✗ Incorrect
has_many through uses a join model where you can add extra fields and validations, unlike has_and_belongs_to_many which uses a simple join table.