2.3 KiB
2.3 KiB
Namespace Conflict Fix
Issue
There was a naming conflict between the Admin model class and the Admin module namespace used by the admin controllers.
TypeError (Admin is not a module
/app/models/admin.rb:1: previous definition of Admin was here)
Solution
Renamed the model from Admin to AdminUser to avoid the namespace conflict.
Changes Made
1. Database Migration
- Created migration:
20251020031401_rename_admins_to_admin_users.rb - Renamed table:
admins→admin_users
2. Model Renamed
- File:
app/models/admin.rb→app/models/admin_user.rb - Class:
Admin→AdminUser
3. Controllers Updated
app/controllers/admin/base_controller.rb: Updatedcurrent_adminmethodapp/controllers/admin/sessions_controller.rb: Updated authentication logic
4. Seeds Updated
db/seeds.rb: ChangedAdmintoAdminUser
5. Tests Updated
- File:
test/models/admin_test.rb→test/models/admin_user_test.rb - Class:
AdminTest→AdminUserTest - Fixtures:
test/fixtures/admins.yml→test/fixtures/admin_users.yml
No View Changes Required
The views remain unchanged because they use helper methods (current_admin, logged_in?) that abstract away the model name.
Migration Command
bin/rails db:migrate
Database Structure
# Before
create_table "admins" do |t|
t.string :email, null: false
t.string :password_digest, null: false
t.string :name, null: false
t.datetime :last_login_at
t.timestamps
end
# After
create_table "admin_users" do |t|
t.string :email, null: false
t.string :password_digest, null: false
t.string :name, null: false
t.datetime :last_login_at
t.timestamps
end
Login Credentials
Unchanged - still:
Email: admin@example.com
Password: password123
API Usage
No changes to the admin interface or API endpoints. Everything works the same from a user perspective.
Technical Notes
- Rails namespaces (modules) take precedence over class names
- Having
module Adminandclass Adminin the same project causes conflicts - The solution is to rename either the module or the class
- We chose to rename the model since it's less invasive than renaming all controllers
Verification
Run this to verify everything works:
bin/rails runner "puts AdminUser.first.email"
Should output: admin@example.com