Make sure foreign keys can be specified as symbols for has_and_belongs_to_many associations.

This commit is contained in:
Matt Gibson 2014-09-10 21:32:46 +01:00 committed by Elliot Winkler
parent 84e9015240
commit 0dd79ced47
3 changed files with 26 additions and 3 deletions

View File

@ -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

View File

@ -51,7 +51,7 @@ module Shoulda
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

View File

@ -757,7 +757,7 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
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 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
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