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 inherited from Hash

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

Instance Method Details

#default_join_tableObject

Default name symbol for the join table.



213
214
215
216
# File 'lib/sequel_model/association_reflection.rb', line 213

def default_join_table
  ([self[:class_name].demodulize, self[:model].name.to_s.demodulize]. \
    map{|i| i.pluralize.underscore}.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).



220
221
222
# File 'lib/sequel_model/association_reflection.rb', line 220

def default_left_key
  :"#{self[:model].name.to_s.demodulize.underscore}_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).



226
227
228
# File 'lib/sequel_model/association_reflection.rb', line 226

def default_right_key
  :"#{self[:name].to_s.singularize}_id"
end

#eager_loader_keyObject

The key to use for the key hash when eager loading



231
232
233
# File 'lib/sequel_model/association_reflection.rb', line 231

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)


237
238
239
# File 'lib/sequel_model/association_reflection.rb', line 237

def need_associated_primary_key?
  true
end

#reciprocalObject

Returns/sets the reciprocal association variable, if one exists



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/sequel_model/association_reflection.rb', line 242

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.



257
258
259
# File 'lib/sequel_model/association_reflection.rb', line 257

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.



262
263
264
265
# File 'lib/sequel_model/association_reflection.rb', line 262

def select
 return self[:select] if include?(:select)
 self[:select] ||= associated_class.table_name.*
end