diff --git a/NEWS.md b/NEWS.md index 63e193113..24b4476b4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,9 +13,14 @@ exist, and the matcher fails, it does not raise an error when producing the failure message. ([#588]) +* Fix `have_and_belong_to_many` used with `join_table` so that it does not fail + when `foreign_key` and/or `association_foreign_key` was specified on the + association as a symbol instead of a string. ([#584]) + [#591]: https://github.com/thoughtbot/shoulda-matchers/pull/591 [#592]: https://github.com/thoughtbot/shoulda-matchers/pull/592 [#588]: https://github.com/thoughtbot/shoulda-matchers/pull/588 +[#584]: https://github.com/thoughtbot/shoulda-matchers/pull/584 # 2.7.0 diff --git a/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb b/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb index 0c9ade18f..bb97e9bc5 100644 --- a/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb @@ -51,7 +51,7 @@ def join_table_has_correct_columns? def missing_columns @missing_columns ||= expected_join_table_columns.select do |key| - !actual_join_table_columns.include?(key) + !actual_join_table_columns.include?(key.to_s) end end diff --git a/spec/shoulda/matchers/active_record/association_matcher_spec.rb b/spec/shoulda/matchers/active_record/association_matcher_spec.rb index a1c49a481..e1526e615 100644 --- a/spec/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/shoulda/matchers/active_record/association_matcher_spec.rb @@ -757,7 +757,7 @@ def having_one_non_existent(model_name, assoc_name, options = {}) expect do expect(Person.new).to have_and_belong_to_many(:relatives) - end.to fail_with_message_including('missing columns: custom_foreign_key_id, relative_id') + end.to fail_with_message_including('missing column: relative_id') end end @@ -776,8 +776,26 @@ def having_one_non_existent(model_name, assoc_name, options = {}) expect do expect(Person.new).to have_and_belong_to_many(:relatives) - end.to fail_with_message_including('missing columns: person_id, custom_association_foreign_key_id') + end.to fail_with_message_including('missing column: person_id') end + + it 'accepts foreign keys when they are symbols' do + define_model :relative + define_model :person do + has_and_belongs_to_many :relatives, + foreign_key: :some_foreign_key_id, + association_foreign_key: :custom_association_foreign_key_id + end + + define_model :people_relative, + id: false, + custom_association_foreign_key_id: :integer, + some_foreign_key_id: :integer + + expect(Person.new).to have_and_belong_to_many(:relatives) + + end + end it 'rejects an association of the wrong type' do