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

Foreign keys can be symbols for has_and_belongs_to_many #584

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# HEAD

* `have_and_belong_to_many` will now pass if the foreign keys have been defined as symbols

# 2.7.0

### Deprecations
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 @@ -747,7 +747,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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these tests failing or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they were. The test setup supplies the custom_foreign_key_id column as a symbol, so it should not be flagged as missing in the error message. Only the relative_id should. Because of #583 the column was not being recognised previously and the error message that the test was expecting was actually a bug as it was reporting both columns missing instead of one. The new error message is the correct one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks for the explanation.

end
end

Expand All @@ -766,8 +766,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