Skip to content

Commit

Permalink
Make sure foreign keys can be specified as symbols for has_and_belong…
Browse files Browse the repository at this point in the history
…s_to_many associations.
  • Loading branch information
mattgibson authored and mcmire committed Oct 9, 2014
1 parent 84e9015 commit 0dd79ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions spec/shoulda/matchers/active_record/association_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 0dd79ce

Please sign in to comment.