Challenge - 5 Problems
Rails Table Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Pluralization of table names in Rails
In Rails, what is the conventional way to name database tables for models?
Attempts:
2 left
💡 Hint
Think about how Rails automatically maps models to tables.
✗ Incorrect
Rails uses pluralized lowercase table names by convention. For example, the User model maps to the 'users' table.
🧠 Conceptual
intermediate2:00remaining
Naming join tables for many-to-many relationships
How should you name a join table for a many-to-many relationship between models 'Author' and 'Book' in Rails?
Attempts:
2 left
💡 Hint
Rails expects join tables to be named alphabetically and pluralized.
✗ Incorrect
Join tables in Rails are named by combining the pluralized model names in alphabetical order separated by an underscore. So 'authors_books' is correct.
❓ query_result
advanced2:00remaining
Effect of incorrect table naming on ActiveRecord queries
Given a Rails model named 'ProductCategory' but the database table is named 'productcategory', what will happen when you run ProductCategory.all?
Attempts:
2 left
💡 Hint
Rails expects table names to be plural and underscored by default.
✗ Incorrect
ActiveRecord expects the table name to be the pluralized, underscored version of the model name. If the table name is incorrect, it raises an error.
❓ schema
advanced2:00remaining
Custom table name declaration in Rails model
How can you specify a custom table name 'inventory_items' for a Rails model named 'Item'?
Attempts:
2 left
💡 Hint
Look for the correct syntax to override the table name in Rails models.
✗ Incorrect
The correct way to set a custom table name is by assigning to self.table_name inside the model class.
❓ optimization
expert3:00remaining
Optimizing queries with table naming and indexing
You have a large Rails app with a table named 'user_sessions' storing millions of records. Which naming and indexing strategy helps optimize queries filtering by 'user_id'?
Attempts:
2 left
💡 Hint
Think about Rails conventions and indexing for query speed.
✗ Incorrect
Keeping the conventional table name and adding an index on the frequently queried column improves query performance without breaking conventions.