From 21a386bb0726cce4f4a5d64d55fbb55d8a2b9837 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 29 Oct 2015 16:46:57 -0600 Subject: 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. --- .../lib/active_record/associations/builder/has_and_belongs_to_many.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb index a5c9f1666e..b888148841 100644 --- a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb @@ -62,13 +62,13 @@ module ActiveRecord::Associations::Builder # :nodoc: end def self.add_left_association(name, options) - belongs_to name, options + belongs_to name, required: false, **options self.left_reflection = _reflect_on_association(name) end def self.add_right_association(name, options) rhs_name = name.to_s.singularize.to_sym - belongs_to rhs_name, options + belongs_to rhs_name, required: false, **options self.right_reflection = _reflect_on_association(rhs_name) end -- cgit v1.2.3