[{"id":"railsadvanced-001","courseId":"rails-advanced","domain":"data-modeling","topic":"Declaring composite primary keys in models","prompt":"A Product model declares a composite primary key of store_id and sku. Which TWO statements about its queries are correct?","hint":"Count how many values a lookup or an ordering must use when the key has two columns.","explain":"A composite key is several columns that together identify one row, so Rails needs every column to address one record. Default ordering follows the whole key, not a single column.","source":"https://guides.rubyonrails.org/active_record_composite_primary_keys.html#what-are-composite-primary-keys-questionmark","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Product.find([3, 'XYZ12345']) looks up one row by both store_id and sku.","why":"find takes an array with one value per key column, so both columns are matched."},{"id":"b","text":"Product.first orders by store_id, then sku, both ascending.","why":"first sorts by every key column in ascending order and takes the top row."},{"id":"c","text":"Product.find(3) finds the row whose store_id is 3, since that is the first key column.","whyNot":"A single value isn't enough for a composite key; you pass an array with a value for every column."},{"id":"d","text":"Product.last orders by store_id and sku, both ascending, then takes the bottom row.","whyNot":"last reverses the direction of every key column, so both sort descending."},{"id":"e","text":"Product.find_by(id: 5) always matches the full composite primary key.","whyNot":"id matches an :id attribute, which is not the composite key here."},{"id":"f","text":"Product.first orders by sku alone, because sku is the column that looks unique.","whyNot":"Ordering uses all key columns, not just the one that seems unique."}],"answer":"ab","explanation":"find takes one value per key column, and first and last order by every key column: ascending for first, descending for last.","concept":"find takes one value per key column, and first and last order by every key column: ascending for first, descending for last.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["A composite key is addressed and ordered by all of its columns, never by just one."]},{"id":"railsadvanced-002","courseId":"rails-advanced","domain":"data-modeling","topic":"Composite primary keys in migrations","prompt":"On Rails 8.1, which migration creates a products table whose primary key is the pair of store_id and sku?","hint":"The option that names the key should take a list of column names, not a single string.","explain":"A table's key is fixed when the table is created, through an option on the creation call. That option accepts a value of a particular shape, so the way you write it matters.","source":"https://guides.rubyonrails.org/active_record_composite_primary_keys.html#composite-primary-key-migrations","interaction":{"type":"single"},"options":[{"id":"a","text":"create_table :products, primary_key: [:store_id, :sku] do |t| ... end","why":"Passing an array to the primary_key option of create_table declares both columns as the key."},{"id":"b","text":"create_table :products, primary_key: 'store_id, sku' do |t| ... end","whyNot":"The option takes an array of column names; a single string names one column, not two."},{"id":"c","text":"create_table :products, id: false, then add a unique index on [:store_id, :sku]","whyNot":"A unique index enforces uniqueness but doesn't declare the table's primary key."},{"id":"d","text":"create_table :products, composite_key: [:store_id, :sku] do |t| ... end","whyNot":"There is no composite_key option; the key is declared through primary_key."}],"answer":"a","explanation":"create_table accepts a primary_key option that takes an array of column names to declare a composite primary key.","concept":"create_table accepts a primary_key option that takes an array of column names to declare a composite primary key.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Declare a composite key when the table is created, with an array-valued primary_key option."]},{"id":"railsadvanced-003","courseId":"rails-advanced","domain":"data-modeling","topic":"Halting callbacks with throw :abort","prompt":"Rails runs the callbacks for creating and saving a new record in a fixed sequence. Put these callbacks in the order Rails calls them.","hint":"Work out which hooks sit before the database write and which sit after it.","explain":"Saving a new record passes through a fixed series of hooks. Some check the record, some wrap the write itself, and some run once the new row is stored and the surrounding transaction is settled.","source":"https://guides.rubyonrails.org/active_record_callbacks.html#halting-execution","interaction":{"type":"sort","items":[{"text":"before_validation","why":"Runs first, before validation checks the record."},{"text":"before_save","why":"Runs after validation passes and before any create-specific hook."},{"text":"before_create","why":"Runs just before the INSERT, so the record can still be changed directly."},{"text":"after_create","why":"Runs once the new row has been inserted."},{"text":"after_commit","why":"Runs last, after the surrounding transaction has committed."}]},"options":[],"answer":"o:abcde","explanation":"Callbacks for creating a record run in the order the guide lists them: before_validation, before_save, before_create, after_create, then after_commit / after_rollback.","concept":"Callbacks for creating a record run in the order the guide lists them: before_validation, before_save, before_create, after_create, then after_commit / after_rollback.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Create callbacks run in a fixed order; know where your hook sits before relying on it."]},{"id":"railsadvanced-004","courseId":"rails-advanced","domain":"data-modeling","topic":"Ciphertext storage for encrypted attributes","prompt":"A team declares encrypts :title on Article. A developer runs raw SQL on the articles table and reads the title column. What does that column contain?","hint":"Think about what decryption needs beyond the scrambled bytes.","explain":"Encryption at this layer changes what the database stores, not only how the attribute is read. Decrypting later needs more than the scrambled bytes, so the stored form carries extra fields.","source":"https://guides.rubyonrails.org/active_record_encryption.html#declare-encrypted-attributes","interaction":{"type":"single"},"options":[{"id":"a","text":"A JSON object with a p key holding the ciphertext and an h key holding metadata such as iv and at.","why":"p carries the encrypted payload; h carries what decryption needs, including the initialization vector and authentication tag."},{"id":"b","text":"The plain title, with the ciphertext kept in a separate hidden column.","whyNot":"encrypts changes the stored value itself, so no plain copy sits beside it."},{"id":"c","text":"A Base64 string of the ciphertext, with no decryption metadata stored alongside.","whyNot":"Decryption needs metadata such as the initialization vector, and it is stored inside the same value."},{"id":"d","text":"A SHA-256 hash of the title, which can never be turned back into the original text.","whyNot":"Encryption is reversible with the keys; a one-way hash would make the attribute impossible to read back."}],"answer":"a","explanation":"encrypts stores the value as a JSON object: p holds the compressed, Base64-encoded ciphertext, and h holds the iv and at needed to decrypt and check it.","concept":"encrypts stores the value as a JSON object: p holds the compressed, Base64-encoded ciphertext, and h holds the iv and at needed to decrypt and check it.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["encrypts replaces the stored value with a JSON envelope holding the payload and decryption headers."]},{"id":"railsadvanced-005","courseId":"rails-advanced","domain":"data-modeling","topic":"Querying non-deterministic encrypted attributes","prompt":"An Article model declares encrypts :title. In the Rails console, Article.first prints title as [FILTERED], even though the row has a value. What is happening?","hint":"Consider where secrets could leak if the console printed the decrypted value.","explain":"An encrypted attribute is still read and written like any other attribute, so code receives plain values. Printing an object for inspection is a separate path that can expose data, so it deserves its own thought.","source":"https://guides.rubyonrails.org/active_record_encryption.html#querying-encrypted-data-deterministic-vs-non-deterministic-encryption","interaction":{"type":"single"},"options":[{"id":"a","text":"The console hides the decrypted title, so the placeholder [FILTERED] is shown instead of its value.","why":"The guide shows the Rails console printing [FILTERED] for the encrypted title, even though the row holds a value."},{"id":"b","text":"The stored title is corrupted, so Rails shows a placeholder instead.","whyNot":"Nothing is corrupted; reading the attribute in code returns the decrypted value."},{"id":"c","text":"Rails hides every attribute in the console, so id and created_at would show as [FILTERED] too.","whyNot":"The same console output shows id and created_at unfiltered; only the encrypted title is hidden."},{"id":"d","text":"The articles table has no title column, so Rails fills in the placeholder.","whyNot":"Encrypted attributes are backed by a column with the same name, so the column exists."}],"answer":"a","explanation":"Declaring an attribute with encrypts keeps it an ordinary attribute in code, while the Rails console shows an encrypted attribute as [FILTERED] instead of its decrypted value.","concept":"Declaring an attribute with encrypts keeps it an ordinary attribute in code, while the Rails console shows an encrypted attribute as [FILTERED] instead of its decrypted value.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Encrypted attributes read normally in code; the console shows them as [FILTERED]."]}]