Complete the code to declare a belongs_to association in a Rails model.
class Comment < ApplicationRecord [1] :post end
The belongs_to method sets up a one-to-one connection with another model, indicating that each comment belongs to a single post.
Complete the code to add a foreign key column for the belongs_to association in a Rails migration.
class AddPostRefToComments < ActiveRecord::Migration[7.0] def change add_reference :comments, :[1], foreign_key: true end end
The foreign key column should match the associated model name, which is post in this case.
Fix the error in the belongs_to association declaration.
class Order < ApplicationRecord [1] :customer, optional: true end
The correct method to declare the association is belongs_to. The option optional: true allows the association to be nil.
Fill both blanks to complete the belongs_to association with a custom class name and foreign key.
class Review < ApplicationRecord belongs_to :[1], class_name: '[2]', foreign_key: 'author_id' end
The association name is author, and the class name must be the exact model name 'Author' as a string.
Fill all three blanks to complete a belongs_to association with a polymorphic interface.
class Picture < ApplicationRecord belongs_to :[1], polymorphic: [2], optional: [3] end
The association name is imageable. The polymorphic option expects a boolean true. The optional option is a boolean false here.