What is the general rule for naming database tables in Rails?
Tables should be named in plural form using lowercase letters and underscores to separate words, for example, users or order_items.
Click to reveal answer
beginner
Why does Rails use plural table names by default?
Rails uses plural table names to match the convention that a table holds many records of a model, which is usually singular. For example, the User model corresponds to the users table.
Click to reveal answer
beginner
How should multiple words be combined in a Rails table name?
Use lowercase letters with underscores between words, called snake_case. For example, order_items instead of OrderItems or orderitems.
Click to reveal answer
intermediate
What happens if you name a table in singular form in Rails?
Rails may not automatically map the table to the model correctly, causing errors or requiring manual configuration to specify the table name.
Click to reveal answer
beginner
Give an example of a correct table name for a model named InvoiceItem.
The correct table name would be invoice_items, using plural form and snake_case.
Click to reveal answer
In Rails, what is the correct table name for a model named ProductCategory?
Aproductcategorys
BProductCategories
Cproductcategory
Dproduct_categories
✗ Incorrect
Rails expects table names to be plural and snake_case, so product_categories is correct.
Why should Rails table names be plural?
ABecause tables store multiple records
BBecause models are plural
CBecause Rails does not support singular names
DBecause SQL requires plural table names
✗ Incorrect
Tables hold many records, so plural names reflect this collection.
Which of these is NOT a recommended Rails table naming convention?
AUsing snake_case
BUsing plural names
CUsing camelCase
DUsing lowercase letters
✗ Incorrect
Rails uses snake_case, not camelCase, for table names.
What problem might occur if a table is named in singular form?
ARails may not map the model to the table correctly
BRails will automatically fix it
CThe database will reject the table
DThere is no problem
✗ Incorrect
Rails expects plural table names to map models correctly; singular names can cause mapping issues.
How should you separate words in a Rails table name?
AWith spaces
BWith underscores
CWith hyphens
DNo separation, just camelCase
✗ Incorrect
Rails uses underscores to separate words in table names (snake_case).
Explain the Rails convention for naming database tables and why it is important.
Think about how Rails connects models and tables automatically.
You got /5 concepts.
Describe what could happen if you do not follow Rails table naming conventions.
Consider how Rails expects table names to relate to model names.
You got /4 concepts.
Practice
(1/5)
1. In Rails, what is the correct way to name a database table for storing multiple user records?
easy
A. users
B. User
C. userTable
D. UsersTable
Solution
Step 1: Understand Rails table naming convention
Rails expects table names to be plural and lowercase to represent collections of records.
Step 2: Apply naming to the user entity
The plural form of "user" is "users", all lowercase, matching Rails conventions.
Final Answer:
users -> Option A
Quick Check:
Plural lowercase table name = users [OK]
Hint: Table names are plural and lowercase with underscores [OK]
Common Mistakes:
Using singular names like 'User'
Capitalizing table names
Adding extra words like 'Table'
2. Which of the following is the correct Rails table name for a model named OrderItem?
easy
A. OrderItems
B. orderitems
C. order_items
D. order-Items
Solution
Step 1: Identify word separation in table names
Rails uses underscores to separate words in table names, all lowercase.
Step 2: Convert 'OrderItem' to table name
Split words with underscore and pluralize: 'order_items'.
Final Answer:
order_items -> Option C
Quick Check:
Use underscores and plural lowercase = order_items [OK]
Hint: Use underscores between words, all lowercase [OK]
Common Mistakes:
Writing names without underscores
Using camel case or hyphens
Capitalizing letters in table names
3. Given a Rails model named BlogPost, which table name will Rails expect to find in the database?
medium
A. blog_posts
B. blogposts
C. BlogPosts
D. blog-posts
Solution
Step 1: Recall Rails naming rules for tables
Tables are plural, lowercase, and use underscores to separate words.
Step 2: Apply rules to 'BlogPost'
Split words with underscore and pluralize: 'blog_posts'.
Final Answer:
blog_posts -> Option A
Quick Check:
Plural, lowercase, underscores = blog_posts [OK]
Hint: Pluralize and underscore multiword model names [OK]
Common Mistakes:
Using camel case or hyphens
Not pluralizing the table name
Capitalizing table names
4. You have a table named user_profiles but your Rails model is named UserProfile. You get an error that the table does not exist. What is the likely cause?
medium
A. The table name should be singular: user_profile
B. The table name is correct; the error is elsewhere
C. The table name should be plural and lowercase with underscores, which it is
D. The table name should be camel case: UserProfiles
Solution
Step 1: Check Rails table naming conventions
Rails expects plural, lowercase, underscore-separated table names matching the model name.
Step 2: Verify the table name 'user_profiles'
This matches the convention for model 'UserProfile'. So the table name is correct.
Final Answer:
The table name is correct; the error is elsewhere -> Option B
Quick Check:
Table name matches Rails convention = user_profiles [OK]
Hint: Rails table names are plural lowercase with underscores [OK]
Common Mistakes:
Changing table name to singular
Using camel case for table names
Assuming error is table name when it is not
5. You want to create a join table for a many-to-many relationship between Author and Book models in Rails. What is the correct table name?
hard
A. author_books
B. AuthorsBooks
C. author_book
D. authors_books
Solution
Step 1: Understand Rails join table naming
Join tables are named by combining the plural names of both models in alphabetical order, separated by an underscore.
Step 2: Apply to 'Author' and 'Book'
Plural forms are 'authors' and 'books'. Alphabetically, 'authors' comes before 'books', so the table name is 'authors_books'.
Final Answer:
authors_books -> Option D
Quick Check:
Join table = plural model names in alphabetical order [OK]
Hint: Join tables use plural model names alphabetically joined by underscore [OK]
Common Mistakes:
Using singular names in join table
Not ordering names alphabetically
Capitalizing join table names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.