Class: Sequel::Model::Associations::ManyToManyAssociationReflection

Inherits:
AssociationReflection show all
Defined in:
lib/sequel/model/association_reflection.rb

Instance Method Summary collapse

Methods inherited from AssociationReflection

#_add_method, #_dataset_method, #_remove_all_method, #_remove_method, #_setter_method, #add_method, #associated_class, #association_method, #dataset_helper_method, #dataset_method, #dataset_need_primary_key?, #eager_graph_lazy_dataset?, #reciprocal_array?, #remove_all_method, #remove_method, #returns_array?, #set_reciprocal_to_self?, #setter_method

Methods included from Inflections

clear, irregular, plural, singular, uncountable

Methods inherited from Hash

#&, #case, #sql_expr, #sql_negate, #sql_or, #|, #~

Instance Method Details

#default_join_tableObject

Default name symbol for the join table.



210
211
212
# File 'lib/sequel/model/association_reflection.rb', line 210

def default_join_table
  [self[:class_name], self[:model].name].map{|i| underscore(pluralize(demodulize(i)))}.sort.join('_').to_sym
end

#default_left_keyObject

Default foreign key name symbol for key in join table that points to current table’s primary key (or :left_primary_key column).



216
217
218
# File 'lib/sequel/model/association_reflection.rb', line 216

def default_left_key
  :"#{underscore(demodulize(self[:model].name))}_id"
end

#default_right_keyObject

Default foreign key name symbol for foreign key in join table that points to the association’s table’s primary key (or :right_primary_key column).



222
223
224
# File 'lib/sequel/model/association_reflection.rb', line 222

def default_right_key
  :"#{singularize(self[:name])}_id"
end

#eager_loader_keyObject

The key to use for the key hash when eager loading



227
228
229
# File 'lib/sequel/model/association_reflection.rb', line 227

def eager_loader_key
  self[:left_primary_key]
end

#need_associated_primary_key?Boolean

Whether the associated object needs a primary key to be added/removed, true for many_to_many associations.

Returns:

  • (Boolean)


233
234
235
# File 'lib/sequel/model/association_reflection.rb', line 233

def need_associated_primary_key?
  true
end

#reciprocalObject

Returns/sets the reciprocal association variable, if one exists



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/sequel/model/association_reflection.rb', line 238

def reciprocal
  return self[:reciprocal] if include?(:reciprocal)
  left_key = self[:left_key]
  right_key = self[:right_key]
  join_table = self[:join_table]
  associated_class.all_association_reflections.each do |assoc_reflect|
    if assoc_reflect[:type] == :many_to_many && assoc_reflect[:left_key] == right_key \
       && assoc_reflect[:right_key] == left_key && assoc_reflect[:join_table] == join_table
      return self[:reciprocal] = assoc_reflect[:name]
    end
  end
  self[:reciprocal] = nil
end

#right_primary_keyObject

The primary key column to use in the associated table.



253
254
255
# File 'lib/sequel/model/association_reflection.rb', line 253

def right_primary_key
  self[:right_primary_key] ||= associated_class.primary_key
end

#selectObject

The columns to select when loading the association, associated_class.table_name.* by default.



258
259
260
261
# File 'lib/sequel/model/association_reflection.rb', line 258

def select
 return self[:select] if include?(:select)
 self[:select] ||= Sequel::SQL::ColumnAll.new(associated_class.table_name)
end