0
0
Ruby on Railsframework~20 mins

Many-to-many with has_many through in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2: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)
AAn array containing only post1 object
BAn empty array
CAn array containing only post2 object
DAn array containing post1 and post2 objects
Attempts:
2 left
💡 Hint
Think about how has_many through works to connect user and posts via user_posts.
📝 Syntax
intermediate
2: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?
A
class Author &lt; ApplicationRecord
  has_many :books
  has_many :authorships, through: :books
end
B
class Author &lt; ApplicationRecord
  has_many :authorships
  has_many :books, through: :authorships
end
C
class Author &lt; ApplicationRecord
  has_many :authorships, through: :books
  has_many :books
end
D
class Author &lt; ApplicationRecord
  has_many :books, through: :authorships
end
Attempts:
2 left
💡 Hint
Remember the join model should be declared first, then the through association.
🔧 Debug
advanced
2:00remaining
Why does user.posts return empty?
Given these models:

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?
ABecause no UserPost record links the user and post
BBecause the has_many :posts association is missing
CBecause the User model should use has_and_belongs_to_many instead
DBecause the Post model lacks a belongs_to :user
Attempts:
2 left
💡 Hint
Check if the join table has any records connecting user and post.
state_output
advanced
2:00remaining
Effect of destroying a join record
If a UserPost record linking a user and a post is destroyed, what happens to user.posts?
Auser.posts still includes the post because the association caches it
BThe post record is deleted from the database
CThe post is removed from user.posts but the post record remains in the database
DAn error is raised when accessing user.posts
Attempts:
2 left
💡 Hint
Think about what destroying a join record affects in a many-to-many association.
🧠 Conceptual
expert
2: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?
Ahas_many through allows adding extra attributes to the join model
Bhas_and_belongs_to_many supports validations on the join table
Chas_many through automatically creates the join table without migration
Dhas_and_belongs_to_many is deprecated and no longer works
Attempts:
2 left
💡 Hint
Think about what you can do with the join model in has_many through.