Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inflected table name wrong for very long model names #1480

Open
martingregoire opened this issue Jan 19, 2022 · 0 comments
Open

Inflected table name wrong for very long model names #1480

martingregoire opened this issue Jan 19, 2022 · 0 comments

Comments

@martingregoire
Copy link

When having two models with very long model names, and creating a HABTM relation between them, the join table name inflected by Rails is shortened (depending on the underlying database). For PostgreSQL the table name has a maximum length of 63 characters.

For Rails this join table name is shortened automatically. For shoulda-matchers we have to explicitly define the join_table option on the relation, otherwise shoulda-matchers will report an error.

Can the inflection in shoulda-matchers include the table name length constraints used by Rails?


Example: these are our models:

module DataProtection
  class ProcessingActivity < ApplicationRecord
    has_and_belongs_to_many :technical_and_organisational_security_measures
  end
end

module DataProtection
  class TechnicalAndOrganisationalSecurityMeasure < ApplicationRecord
    has_and_belongs_to_many :processing_activities
  end
end

The schema.rb contains the shortened join table name for PostgreSQL:

  create_table "data_protection_processing_activities_technical_and_organisatio", force: :cascade do |t|
    t.integer "processing_activity_id", null: false
    t.integer "technical_and_organisational_security_measure_id", null: false
  end

When using these matchers in the model specs:

describe DataProtection::ProcessingActivity do
  it { should have_and_belong_to_many(:technical_and_organisational_security_measures) }
end

shoulda-matchers fails with:

Expected DataProtection::ProcessingActivity to have a has_and_belongs_to_many association called
technical_and_organisational_security_measures (join table
data_protection_processing_activities_technical_and_organisational_security_measures doesn't exist)

We tried adding the correct join table name option to the matcher:

  it { should have_and_belong_to_many(:technical_and_organisational_security_measures).join_table('data_protection_processing_activities_technical_and_organisatio') }

Now shoulda-matchers fails with:

Expected DataProtection::ProcessingActivity to have a has_and_belongs_to_many association called
technical_and_organisational_security_measures (technical_and_organisational_security_measures should use
"data_protection_processing_activities_technical_and_organisatio" for :join_table option)

This forces us to explicitly add the join_table option to the model, which is not necessary:

has_and_belongs_to_many :technical_and_organisational_security_measures, join_table: :data_protection_processing_activities_technical_and_organisatio

Now the matcher does not report an error any longer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant