Complete the code to declare a polymorphic association in a Rails model.
class Comment < ApplicationRecord belongs_to :[1], polymorphic: true end
The association name commentable is the conventional name used for polymorphic belongs_to in Rails.
Complete the code to declare a polymorphic has_many association in a Rails model.
class Post < ApplicationRecord has_many :[1], as: :commentable end
The has_many association should be plural and match the polymorphic association name in the related model.
Fix the error in the migration to create a polymorphic reference.
create_table :comments do |t|
t.references :[1], polymorphic: true, null: false
t.text :content
t.timestamps
endThe reference name must match the polymorphic association name, which is commentable.
Fill both blanks to complete the polymorphic association in the model and migration.
class Picture < ApplicationRecord belongs_to :[1], polymorphic: true end create_table :pictures do |t| t.references :[2], polymorphic: true, null: false t.string :image_url t.timestamps end
Both the model and migration must use the same polymorphic association name imageable.
Fill all three blanks to create a polymorphic association with validations.
class Tag < ApplicationRecord belongs_to :[1], polymorphic: true validates :[2], presence: true validates :[3], presence: true end
The polymorphic association is named taggable. The validations ensure presence of taggable_id and taggable_type, which are required for polymorphic associations.