[{"id":"railsintermediate-001","courseId":"rails-intermediate","domain":"data-modeling","topic":"belongs_to presence validation","prompt":"A Book model declares belongs_to :author, and the books table has an author_id column. You want the database to refuse books whose author does not exist, and quick lookups of one author's books. Which TWO choices do that?","hint":"Think about which layer enforces a link, and what makes a lookup by column fast.","explain":"A reference column points at a key in another table. Rails can check that link in application code or leave it to the database, and the column can also be indexed for fast lookups.","source":"https://guides.rubyonrails.org/association_basics.html#belongs-to","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"belongs_to :authors, since Rails finds the class from the plural name","whyNot":"belongs_to must use the singular term; the plural makes Rails look for a class named Authors."},{"id":"b","text":"Add foreign_key: true to the reference, so the database requires author_id to match an existing authors id","why":"foreign_key: true makes the database enforce that author_id points at a real row in authors."},{"id":"c","text":"Make the author_id index unique, so each author has only one book","whyNot":"A unique index would cap each author at one book; the guide suggests a non-unique index because an author has many books."},{"id":"d","text":"Add a non-unique index on author_id","why":"An index on author_id speeds up finding one author's books; the guide recommends it be non-unique."},{"id":"e","text":"Rely on belongs_to alone, which makes the database reject unknown author ids","whyNot":"belongs_to sets up the Ruby association; database-level checks need the foreign_key: true option."},{"id":"f","text":"Add a unique index on author_id, or Rails refuses to load the Book model","whyNot":"Rails does not need that to load Book; the guide recommends a non-unique index."}],"answer":"bd","explanation":"Adding foreign_key: true makes the database enforce that author_id matches an authors id, and an index on author_id speeds up lookups of one author's books.","concept":"Adding foreign_key: true makes the database enforce that author_id matches an authors id, and an index on author_id speeds up lookups of one author's books.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Put foreign_key: true on a reference to enforce the link in the database, and index the column for lookups."]},{"id":"railsintermediate-002","courseId":"rails-intermediate","domain":"data-modeling","topic":"has_many :through needs a join model","prompt":"You want each student-course link validated when the student is saved. has_and_belongs_to_many :courses, validate: true is not supported. What is the closest supported design?","hint":"Look for a setup where the link rows are records of their own.","explain":"Each association type takes its own set of options, and some options exist on only one type. A link between two models can be stored as a plain join table or as a model with its own table.","source":"https://guides.rubyonrails.org/association_basics.html#has-many-through","interaction":{"type":"single"},"options":[{"id":"a","text":"Keep has_and_belongs_to_many and add validates :courses, presence: true","whyNot":"That checks that some courses are present; it does not validate the link rows themselves."},{"id":"b","text":"Use has_many :courses with dependent: :destroy","whyNot":"dependent decides what happens to courses when the student is destroyed, not how they are validated."},{"id":"c","text":"has_many :enrollments plus has_many :courses, through: :enrollments, with an enrollments table that has an id","why":"A join table with an id, used through has_many :through, is the supported stand-in for the :validate option that has_and_belongs_to_many lacks."},{"id":"d","text":"Use has_one :course on Student with validate: true","whyNot":"has_one holds a single record, so it cannot link a student to many courses."}],"answer":"c","explanation":"has_and_belongs_to_many does not support the :validate option; a join table used with has_many :through gives similar functionality.","concept":"has_and_belongs_to_many does not support the :validate option; a join table used with has_many :through gives similar functionality.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["If an association option is missing, model the link as a join record with has_many :through."]},{"id":"railsintermediate-003","courseId":"rails-intermediate","domain":"data-modeling","topic":"has_and_belongs_to_many versus has_many :through","prompt":"When a has_and_belongs_to_many association is backed by a migration, its join table should be created without an id column.","hint":"Ask whether the join table stands for a model of its own.","explain":"A join table exists to link the rows of two tables. Whether it needs a primary key of its own depends on whether Rails treats its rows as records of a model.","source":"https://guides.rubyonrails.org/association_basics.html#has-many-through-vs-has-and-belongs-to-many","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"The guide creates the join table with id: false, because the join table does not represent a model."},{"id":"b","text":"False","whyNot":"That is the has_many :through rule: its join table requires an id."}],"answer":"a","explanation":"A has_and_belongs_to_many join table is created with id: false because it does not represent a model; a has_many :through join table requires an id.","concept":"A has_and_belongs_to_many join table is created with id: false because it does not represent a model; a has_many :through join table requires an id.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Create a has_and_belongs_to_many join table with id: false; a has_many :through join table needs an id."]},{"id":"railsintermediate-004","courseId":"rails-intermediate","domain":"data-modeling","topic":"dependent destroy versus delete_all","prompt":"An Author has_many :books, dependent: :destroy. Which TWO statements about the books collection methods are true?","hint":"Compare what each method does to the row, and what the dependent option adds.","explain":"Collection methods either clear the link between rows or remove the rows themselves. The dependent option decides what happens to associated records in some of these calls.","source":"https://guides.rubyonrails.org/association_basics.html#belongs-to","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"author.books.delete(book) sets the book's author_id to NULL, even with dependent: :destroy","whyNot":"With dependent: :destroy, delete destroys the book instead of only clearing its foreign key."},{"id":"b","text":"author.books.destroy(book) removes the book from the database, whatever the dependent option says","why":"collection.destroy runs destroy on each object and ignores the :dependent option."},{"id":"c","text":"author.books.clear deletes every book, because clear follows the dependent option and :destroy deletes each object","why":"The clear method uses the dependent strategy, and dependent: :destroy destroys each associated object."},{"id":"d","text":"author.books.destroy(book) removes the book only when dependent is set to :destroy","whyNot":"destroy always removes the book; the dependent option plays no part in that call."},{"id":"e","text":"author.books.clear sets every book's author_id to NULL, even though the association declares dependent: :destroy","whyNot":"The dependent option sets the clear strategy, so :destroy deletes the books instead of nulling their keys."},{"id":"f","text":"author.books.delete(book) always leaves the book row in place, even with dependent: :destroy","whyNot":"With dependent: :destroy, the delete call destroys the book as well."}],"answer":"bc","explanation":"collection.delete sets foreign keys to NULL unless dependent: :destroy destroys the object; collection.destroy always removes the rows; collection.clear follows the dependent strategy.","concept":"collection.delete sets foreign keys to NULL unless dependent: :destroy destroys the object; collection.destroy always removes the rows; collection.clear follows the dependent strategy.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["collection.destroy always deletes the rows; collection.delete and collection.clear follow the dependent option."]},{"id":"railsintermediate-005","courseId":"rails-intermediate","domain":"data-modeling","topic":"polymorphic association columns","prompt":"A Picture model has belongs_to :imageable, polymorphic: true, and its rows store Product in the type column. You rename the class Product to Item. What must you do for the existing pictures?","hint":"Think about what the type column holds, and whether it lives in the database or in code.","explain":"A polymorphic link stores a record's id plus a second column naming the model it points to. Consider what kind of value that second column holds, and where it is kept.","source":"https://guides.rubyonrails.org/association_basics.html#polymorphic-associations","interaction":{"type":"single"},"options":[{"id":"a","text":"Change only the Ruby code, since the database keeps just record ids","whyNot":"The database also stores class names in the type column, so that data must change too."},{"id":"b","text":"Write a migration that updates the type column from the old class name to the new one","why":"The type column stores class names as data, so existing rows must be rewritten to match Item."},{"id":"c","text":"Nothing: Rails maps the old stored name to the renamed class on load","whyNot":"Rails does not translate old stored names; the stored text has to match the Ruby class name."},{"id":"d","text":"Rename the id column so it matches the new class name","whyNot":"The id column holds the record's key; the class name lives in the type column."}],"answer":"b","explanation":"A polymorphic type column stores class names, so when a class is renamed, the stored values must be migrated to the new name.","concept":"A polymorphic type column stores class names, so when a class is renamed, the stored values must be migrated to the new name.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Renaming a polymorphic model means migrating its _type column values, not just the Ruby class."]}]