박상원
박상원

Reputation: 11

Proper reference naming in namespaced models migration

I have a Rails app with namespaced models and routes:

# config/routes.rb
namespace :bank do
  resources :exams
  resources :exam_sections
  resources :exam_units
  # ...
end


# Migration file
class CreateBankExamUnits < ActiveRecord::Migration[8.0]
  def change
    create_table :bank_exam_units do |t|
      t.references :exam_section, null: false,
        foreign_key: { to_table: :bank_exam_sections }
      # ...
    end
  end
end

I'm confused about the proper way to name the reference in the migration file. Should it be:

1. t.references :exam_section (current)


2. t.references :bank_exam_section

Since I'm using namespace and the table name is bank_exam_sections, which one is the Rails convention?

The models are namespaced like Bank::ExamUnit and Bank::ExamSection.

Thanks in advance!

What I tried: I tried both approaches in my migration:

  1. First with t.references :exam_section - This works but I'm not sure if it's the correct convention

  2. Then tried t.references :bank_exam_section - This also works with the foreign key setting

What I expected: I expected there to be a clear Rails convention for reference naming when using namespaced models, especially since the actual table name includes the namespace prefix (bank_exam_sections).

What actually happened: Both approaches seem to work with the foreign key setting, but I couldn't find clear documentation about which is the proper Rails convention when using namespaced models.

Upvotes: 1

Views: 73

Answers (0)

Related Questions