diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-29 16:46:57 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-29 16:53:49 -0600 |
commit | 21a386bb0726cce4f4a5d64d55fbb55d8a2b9837 (patch) | |
tree | 2938e27f69482aa6f8adaf7d9d0155bf401c20be /activerecord/test | |
parent | 9f93a5efbba3e1cbf0bfa700a17ec8d1ef60d7c6 (diff) | |
download | rails-21a386bb0726cce4f4a5d64d55fbb55d8a2b9837.tar.gz rails-21a386bb0726cce4f4a5d64d55fbb55d8a2b9837.tar.bz2 rails-21a386bb0726cce4f4a5d64d55fbb55d8a2b9837.zip |
Ensure `has_and_belongs_to_many` works with `belongs_to_required_by_default`
Before this commit, if
`ActiveRecord::Base.belongs_to_required_by_default` is set to `true`,
then creating a record through `has_and_belongs_to_many` fails with the
cryptic error message `Left side must exist`. This is because
`inverse_of` isn't working properly in this case, presumably since we're
doing trickery with anonymous classes in the middle.
Rather than following this rabbit hole to try and get `inverse_of` to
work in a case that we know is not publicly supported, we can just turn
off this validation to match the behavior of 4.2 and earlier.
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/models/project.rb | 8 |
2 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index e9f679e6de..ccb062f991 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -976,4 +976,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert preloaded_first_project.salaried_developers.loaded?, true assert_equal first_project.salaried_developers.size, preloaded_first_project.salaried_developers.size end + + def test_has_and_belongs_to_many_is_useable_with_belongs_to_required_by_default + assert_difference "Project.first.developers_required_by_default.size", 1 do + Project.first.developers_required_by_default.create!(name: "Sean", salary: 50000) + end + end end diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index b034e0e267..efa8246f1e 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -15,6 +15,14 @@ class Project < ActiveRecord::Base belongs_to :firm has_one :lead_developer, through: :firm, inverse_of: :contracted_projects + begin + previous_value, ActiveRecord::Base.belongs_to_required_by_default = + ActiveRecord::Base.belongs_to_required_by_default, true + has_and_belongs_to_many :developers_required_by_default, class_name: "Developer" + ensure + ActiveRecord::Base.belongs_to_required_by_default = previous_value + end + attr_accessor :developers_log after_initialize :set_developers_log |