Class | Sequel::Model::Associations::AssociationReflection
In: | lib/sequel/model/associations.rb
Parent: | Hash

AssociationReflection is a Hash subclass that keeps information on Sequel::Model associations. It provides methods to reduce internal code duplication. It should not be instantiated by the user.

### Methods

_add_method _dataset_method _remove_all_method _remove_method _setter_method add_method associated_class association_method can_have_associated_objects? dataset_helper_method dataset_method dataset_need_primary_key? eager_graph_lazy_dataset? eager_limit_strategy eager_loader_key eager_loading_predicate_key eager_loading_use_associated_key? limit_and_offset need_associated_primary_key? predicate_keys qualify qualify_assoc qualify_cur reciprocal reciprocal_array? remove_all_method remove_before_destroy? remove_method remove_should_check_existing? returns_array? select set_reciprocal_to_self? setter_method

### Included Modules

Sequel::Inflections

### Public Instance methods

_add_method()

Name symbol for the _add internal association method

[Source]

        # File lib/sequel/model/associations.rb, line 21
    21:         def _add_method
    22:
    23:           "_add_#{singularize(self[:name])}"
    24:         end

_dataset_method()

Name symbol for the _dataset association method

[Source]

        # File lib/sequel/model/associations.rb, line 26
    26:         def _dataset_method
    27:
    28:           "_#{self[:name]}_dataset"
    29:         end

_remove_all_method()

Name symbol for the _remove_all internal association method

[Source]

        # File lib/sequel/model/associations.rb, line 31
    31:         def _remove_all_method
    32:
    33:           "_remove_all_#{self[:name]}"
    34:         end

_remove_method()

Name symbol for the _remove internal association method

[Source]

        # File lib/sequel/model/associations.rb, line 36
    36:         def _remove_method
    37:
    38:           "_remove_#{singularize(self[:name])}"
    39:         end

_setter_method()

Name symbol for the _setter association method

[Source]

        # File lib/sequel/model/associations.rb, line 41
    41:         def _setter_method
    42:
    43:           "_#{self[:name]}="
    44:         end

add_method()

Name symbol for the add association method

[Source]

        # File lib/sequel/model/associations.rb, line 46
    46:         def add_method
    47:
    48:           "add_#{singularize(self[:name])}"
    49:         end

associated_class()

The class associated to the current model class via this association

[Source]

        # File lib/sequel/model/associations.rb, line 56
    56:         def associated_class
    57:           cached_fetch(:class){constantize(self[:class_name])}
    58:         end

association_method()

Name symbol for association method, the same as the name of the association.

[Source]

        # File lib/sequel/model/associations.rb, line 51
    51:         def association_method
    52:           self[:name]
    53:         end

can_have_associated_objects?(obj)

Whether this association can have associated objects, given the current object. Should be false if obj cannot have associated objects because the necessary key columns are NULL.

[Source]

        # File lib/sequel/model/associations.rb, line 63
    63:         def can_have_associated_objects?(obj)
    64:           true
    65:         end

dataset_helper_method()

Name symbol for the _helper internal association method

[Source]

        # File lib/sequel/model/associations.rb, line 73
    73:         def dataset_helper_method
    74:
    75:           "_#{self[:name]}_dataset_helper"
    76:         end

dataset_method()

Name symbol for the dataset association method

[Source]

        # File lib/sequel/model/associations.rb, line 68
    68:         def dataset_method
    69:
    70:           "#{self[:name]}_dataset"
    71:         end

dataset_need_primary_key?()

Whether the dataset needs a primary key to function, true by default.

[Source]

        # File lib/sequel/model/associations.rb, line 78
    78:         def dataset_need_primary_key?
    79:           true
    80:         end

eager_graph_lazy_dataset?()

Whether to eagerly graph a lazy dataset, true by default. If this is false, the association won‘t respect the :eager_graph option when loading the association for a single record.

[Source]

         # File lib/sequel/model/associations.rb, line 122
    122:         def eager_graph_lazy_dataset?
    123:           true
    124:         end

eager_limit_strategy()

The eager limit strategy to use for this dataset.

[Source]

         # File lib/sequel/model/associations.rb, line 83
     83:         def eager_limit_strategy
     84:           cached_fetch(:_eager_limit_strategy) do
     85:             if self[:limit]
     86:               case s = cached_fetch(:eager_limit_strategy){self[:model].default_eager_limit_strategy || :ruby}
     87:               when true
     88:                 ds = associated_class.dataset
     89:                 if ds.supports_window_functions?
     90:                   :window_function
     91:                 else
     92:                   :ruby
     93:                 end
     94:               else
     95:                 s
     96:               end
     97:             else
     98:               nil
     99:             end
    100:           end
    101:         end

eager_loader_key()

The key to use for the key hash when eager loading

[Source]

         # File lib/sequel/model/associations.rb, line 104
    104:         def eager_loader_key
    105:           self[:eager_loader_key]
    106:         end

eager_loading_predicate_key()

Alias of predicate_key, only for backwards compatibility.

[Source]

         # File lib/sequel/model/associations.rb, line 115
    115:         def eager_loading_predicate_key
    116:           predicate_key
    117:         end

eager_loading_use_associated_key?()

By default associations do not need to select a key in an associated table to eagerly load.

[Source]

         # File lib/sequel/model/associations.rb, line 110
    110:         def eager_loading_use_associated_key?
    111:           false
    112:         end

limit_and_offset()

The limit and offset for this association (returned as a two element array).

[Source]

         # File lib/sequel/model/associations.rb, line 127
    127:         def limit_and_offset
    128:           if (v = self[:limit]).is_a?(Array)
    129:             v
    130:           else
    131:             [v, nil]
    132:           end
    133:         end

need_associated_primary_key?()

Whether the associated object needs a primary key to be added/removed, false by default.

[Source]

         # File lib/sequel/model/associations.rb, line 137
    137:         def need_associated_primary_key?
    138:           false
    139:         end

predicate_keys()

The keys to use for loading of the regular dataset, as an array.

[Source]

         # File lib/sequel/model/associations.rb, line 142
    142:         def predicate_keys
    143:           cached_fetch(:predicate_keys){Array(predicate_key)}
    144:         end

qualify(table, col)

Qualify `col` with the given table name. If `col` is an array of columns, return an array of qualified columns. Only qualifies Symbols and SQL::Identifier values, other values are not modified.

[Source]

         # File lib/sequel/model/associations.rb, line 149
    149:         def qualify(table, col)
    150:           transform(col) do |k|
    151:             case k
    152:             when Symbol, SQL::Identifier
    153:               SQL::QualifiedIdentifier.new(table, k)
    154:             else
    155:               Sequel::Qualifier.new(self[:model].dataset, table).transform(k)
    156:             end
    157:           end
    158:         end

qualify_assoc(col)

Qualify col with the associated model‘s table name.

[Source]

         # File lib/sequel/model/associations.rb, line 161
    161:         def qualify_assoc(col)
    162:           qualify(associated_class.table_name, col)
    163:         end

qualify_cur(col)

Qualify col with the current model‘s table name.

[Source]

         # File lib/sequel/model/associations.rb, line 166
    166:         def qualify_cur(col)
    167:           qualify(self[:model].table_name, col)
    168:         end

reciprocal()

Returns the reciprocal association variable, if one exists. The reciprocal association is the association in the associated class that is the opposite of the current association. For example, Album.many_to_one :artist and Artist.one_to_many :albums are reciprocal associations. This information is to populate reciprocal associations. For example, when you do this_artist.add_album(album) it sets album.artist to this_artist.

[Source]

         # File lib/sequel/model/associations.rb, line 176
    176:         def reciprocal
    177:           cached_fetch(:reciprocal) do
    178:             r_types = Array(reciprocal_type)
    179:             keys = self[:keys]
    180:             recip = nil
    181:             associated_class.all_association_reflections.each do |assoc_reflect|
    182:               if r_types.include?(assoc_reflect[:type]) && assoc_reflect[:keys] == keys && assoc_reflect.associated_class == self[:model]
    183:                 cached_set(:reciprocal_type, assoc_reflect[:type])
    184:                 recip = assoc_reflect[:name]
    185:                 break
    186:               end
    187:             end
    188:             recip
    189:           end
    190:         end

reciprocal_array?()

Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.

[Source]

         # File lib/sequel/model/associations.rb, line 194
    194:         def reciprocal_array?
    195:           true
    196:         end

remove_all_method()

Name symbol for the remove_all_ association method

[Source]

         # File lib/sequel/model/associations.rb, line 199
    199:         def remove_all_method
    200:
    201:           "remove_all_#{self[:name]}"
    202:         end

remove_before_destroy?()

Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.

[Source]

         # File lib/sequel/model/associations.rb, line 205
    205:         def remove_before_destroy?
    206:           true
    207:         end

remove_method()

Name symbol for the remove_ association method

[Source]

         # File lib/sequel/model/associations.rb, line 210
    210:         def remove_method
    211:
    212:           "remove_#{singularize(self[:name])}"
    213:         end

remove_should_check_existing?()

Whether to check that an object to be disassociated is already associated to this object, false by default.

[Source]

         # File lib/sequel/model/associations.rb, line 215
    215:         def remove_should_check_existing?
    216:           false
    217:         end

returns_array?()

Whether this association returns an array of objects instead of a single object, true by default.

[Source]

         # File lib/sequel/model/associations.rb, line 221
    221:         def returns_array?
    222:           true
    223:         end

select()

The columns to select when loading the association.

[Source]

         # File lib/sequel/model/associations.rb, line 226
    226:         def select
    227:           self[:select]
    228:         end

set_reciprocal_to_self?()

Whether to set the reciprocal association to self when loading associated records, false by default.

[Source]

         # File lib/sequel/model/associations.rb, line 232
    232:         def set_reciprocal_to_self?
    233:           false
    234:         end

setter_method()

Name symbol for the setter association method

[Source]

         # File lib/sequel/model/associations.rb, line 237
    237:         def setter_method
    238:
    239:           "#{self[:name]}="
    240:         end

[Validate]