Files
MySMSAPio/NAMESPACE_FIX.md
2025-10-22 17:22:17 +08:00

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: adminsadmin_users

2. Model Renamed

  • File: app/models/admin.rbapp/models/admin_user.rb
  • Class: AdminAdminUser

3. Controllers Updated

  • app/controllers/admin/base_controller.rb: Updated current_admin method
  • app/controllers/admin/sessions_controller.rb: Updated authentication logic

4. Seeds Updated

  • db/seeds.rb: Changed Admin to AdminUser

5. Tests Updated

  • File: test/models/admin_test.rbtest/models/admin_user_test.rb
  • Class: AdminTestAdminUserTest
  • Fixtures: test/fixtures/admins.ymltest/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 Admin and class Admin in 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