Class: ActiveRecord::Associations::Builder::HasAndBelongsToMany
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb |
Class Method Summary
Instance Attribute Summary
- #association_name readonly
- #lhs_model readonly
- #options readonly
Instance Method Summary
- #middle_reflection(join_model)
- #through_model
- #belongs_to_options(options) private
- #middle_options(join_model) private
- #table_name private
Constructor Details
.new(association_name, lhs_model, options) ⇒ HasAndBelongsToMany
# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 7
def initialize(association_name, lhs_model, ) @association_name = association_name @lhs_model = lhs_model @options = end
Instance Attribute Details
#association_name (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 5
attr_reader :lhs_model, :association_name, :
#lhs_model (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 5
attr_reader :lhs_model, :association_name, :
#options (readonly)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 5
attr_reader :lhs_model, :association_name, :
Instance Method Details
#belongs_to_options(options) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 92
def ( ) = {} if .key? :class_name [:foreign_key] = [:class_name].to_s.foreign_key [:class_name] = [:class_name] end if .key? :association_foreign_key [:foreign_key] = [:association_foreign_key] end end
#middle_options(join_model) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 71
def (join_model) = {} [:class_name] = "#{lhs_model.name}::#{join_model.name}" if .key? :foreign_key [:foreign_key] = [:foreign_key] end end
#middle_reflection(join_model)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 59
def middle_reflection(join_model) middle_name = [lhs_model.name.downcase.pluralize, association_name.to_s].sort.join("_").gsub("::", "_").to_sym = join_model HasMany.create_reflection(lhs_model, middle_name, nil, ) end
#table_name (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 80
def table_name if [:join_table] [:join_table].to_s else class_name = .fetch(:class_name) { association_name.to_s.camelize.singularize } klass = lhs_model.send(:compute_type, class_name.to_s) [lhs_model.table_name, klass.table_name].sort.join("\0").gsub(/^(.*[._])(.)\0\1(.)/, '\1\2_\3').tr("\0", "_") end end
#through_model
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 13
def through_model join_model = Class.new(ActiveRecord::Base) { class << self attr_accessor :left_model attr_accessor :name attr_accessor :table_name_resolver attr_accessor :left_reflection attr_accessor :right_reflection end @table_name = nil def self.table_name # Table name needs to be resolved lazily # because RHS class might not have been loaded @table_name ||= table_name_resolver.call end def self.compute_type(class_name) left_model.compute_type class_name end def self.add_left_association(name, ) belongs_to name, required: false, ** self.left_reflection = _reflect_on_association(name) end def self.add_right_association(name, ) rhs_name = name.to_s.singularize.to_sym belongs_to rhs_name, required: false, ** self.right_reflection = _reflect_on_association(rhs_name) end def self.connection_pool left_model.connection_pool end } join_model.name = "HABTM_#{association_name.to_s.camelize}" join_model.table_name_resolver = -> { table_name } join_model.left_model = lhs_model join_model.add_left_association :left_side, anonymous_class: lhs_model join_model.add_right_association association_name, ( ) join_model end