diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations.rb | 13 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb | 15 |
2 files changed, 15 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 6ceefdf879..69ed7cc078 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1574,18 +1574,9 @@ module ActiveRecord join_model = builder.through_model - middle_name = [self.name.downcase.pluralize, name].join('_').gsub(/::/, '_').to_sym - - middle_options = builder.middle_options join_model - - hm_builder = Builder::HasMany.create_builder(self, - middle_name, - nil, - middle_options) - middle_reflection = hm_builder.build self + middle_reflection = builder.middle_reflection join_model Builder::HasMany.define_callbacks self, middle_reflection - Reflection.add_reflection self, middle_reflection.name, middle_reflection include Module.new { @@ -1599,7 +1590,7 @@ module ActiveRecord } hm_options = {} - hm_options[:through] = middle_name + hm_options[:through] = middle_reflection.name hm_options[:source] = join_model.right_association_name [:before_add, :after_add, :before_remove, :after_remove].each do |k| 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 4b47824148..2f79976594 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 @@ -80,6 +80,19 @@ module ActiveRecord::Associations::Builder join_model end + def middle_reflection(join_model) + middle_name = [lhs_model.name.downcase.pluralize, + association_name].join('_').gsub(/::/, '_').to_sym + middle_options = middle_options join_model + hm_builder = HasMany.create_builder(lhs_model, + middle_name, + nil, + middle_options) + hm_builder.build lhs_model + end + + private + def middle_options(join_model) middle_options = {} middle_options[:class] = join_model @@ -90,8 +103,6 @@ module ActiveRecord::Associations::Builder middle_options end - private - def belongs_to_options(options) rhs_options = {} |