Class: ActiveRecord::Associations::Builder::HasAndBelongsToMany
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::Builder::HasAndBelongsToMany
- Defined in:
- lib/active_record/associations/builder/has_and_belongs_to_many.rb
Overview
:nodoc:
Defined Under Namespace
Classes: JoinTableResolver
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#lhs_model ⇒ Object
readonly
Returns the value of attribute lhs_model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(association_name, lhs_model, options) ⇒ HasAndBelongsToMany
constructor
A new instance of HasAndBelongsToMany.
- #middle_reflection(join_model) ⇒ Object
- #through_model ⇒ Object
Constructor Details
#initialize(association_name, lhs_model, options) ⇒ HasAndBelongsToMany
Returns a new instance of HasAndBelongsToMany.
40 41 42 43 44 |
# File 'lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 40 def initialize(association_name, lhs_model, ) @association_name = association_name @lhs_model = lhs_model @options = end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
38 39 40 |
# File 'lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 38 def association_name @association_name end |
#lhs_model ⇒ Object (readonly)
Returns the value of attribute lhs_model.
38 39 40 |
# File 'lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 38 def lhs_model @lhs_model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
38 39 40 |
# File 'lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 38 def @options end |
Instance Method Details
#middle_reflection(join_model) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 97 def middle_reflection(join_model) middle_name = [lhs_model.name.downcase.pluralize, association_name].join("_".freeze).gsub("::".freeze, "_".freeze).to_sym = join_model HasMany.create_reflection(lhs_model, middle_name, nil, ) end |
#through_model ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_record/associations/builder/has_and_belongs_to_many.rb', line 46 def through_model habtm = JoinTableResolver.build lhs_model, association_name, 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 def self.table_name table_name_resolver.join_table 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.retrieve_connection left_model.retrieve_connection end private def self.suppress_composite_primary_key(pk) pk unless pk.is_a?(Array) end } join_model.name = "HABTM_#{association_name.to_s.camelize}" join_model.table_name_resolver = habtm 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 |