0
0
Ruby on Railsframework~20 mins

Polymorphic associations in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Polymorphic Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does polymorphic association behave in Rails?

Given a polymorphic association where Comment belongs to commentable which can be either Post or Image, what will comment.commentable return if the comment belongs to a post?

Ruby on Rails
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

class Post < ApplicationRecord
  has_many :comments, as: :commentable
end

post = Post.create(title: "Hello")
comment = Comment.create(body: "Nice post!", commentable: post)

result = comment.commentable
AThe Post object associated with the comment
BThe Comment object itself
CAn array of all comments for the post
DA string with the post's title
Attempts:
2 left
💡 Hint

Think about what belongs_to :commentable means in Rails.

📝 Syntax
intermediate
2:00remaining
Identify the correct polymorphic association syntax

Which option correctly defines a polymorphic association in Rails?

A
class Picture &lt; ApplicationRecord
  belongs_to :imageable
end
B
class Picture &lt; ApplicationRecord
  has_many :imageable, polymorphic: true
end
C
class Picture &lt; ApplicationRecord
  belongs_to :imageable, polymorphic: true
end
D
class Picture &lt; ApplicationRecord
  has_one :imageable, polymorphic: true
end
Attempts:
2 left
💡 Hint

Remember that the model holding the foreign key uses belongs_to with polymorphic: true.

🔧 Debug
advanced
2:00remaining
Why does this polymorphic association raise an error?

Given the following code, why does comment.commentable raise ActiveRecord::RecordNotFound?

Ruby on Rails
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

comment = Comment.create(body: "Test", commentable_type: "Post", commentable_id: 9999)
comment.commentable
ABecause belongs_to requires inverse_of option for polymorphic
BBecause there is no Post record with id 9999 in the database
CBecause commentable_type should be lowercase 'post'
DBecause the polymorphic association is missing the :dependent option
Attempts:
2 left
💡 Hint

Check if the associated record exists in the database.

state_output
advanced
2:00remaining
What is the count of comments for a polymorphic association?

Given a Post with 3 comments and an Image with 2 comments, what is the output of Comment.where(commentable_type: 'Post').count?

Ruby on Rails
post = Post.create(title: "Post")
image = Image.create(name: "Image")
3.times { Comment.create(body: "Post comment", commentable: post) }
2.times { Comment.create(body: "Image comment", commentable: image) }

result = Comment.where(commentable_type: 'Post').count
A3
B5
C2
D0
Attempts:
2 left
💡 Hint

Filter comments by the commentable_type column.

🧠 Conceptual
expert
2:00remaining
Why use polymorphic associations in Rails?

Which is the best explanation for why polymorphic associations are useful in Rails?

AThey replace the need for join tables in many-to-many relationships
BThey enforce strict foreign key constraints between models
CThey automatically generate database indexes for faster queries
DThey allow a model to belong to more than one other model on a single association
Attempts:
2 left
💡 Hint

Think about how one model can be linked to different models using the same association.