class Sequel::Model::Associations::ManyToOneAssociationReflection

Constants

FINALIZE_SETTINGS

Public Instance Methods

associated_object_keys()
Alias for: primary_keys
can_have_associated_objects?(obj) click to toggle source

many_to_one associations can only have associated objects if none of the :keys options have a nil value.

# File lib/sequel/model/associations.rb, line 805
def can_have_associated_objects?(obj)
  !self[:keys].any?{|k| obj.get_column_value(k).nil?}
end
dataset_need_primary_key?() click to toggle source

Whether the dataset needs a primary key to function, false for many_to_one associations.

# File lib/sequel/model/associations.rb, line 810
def dataset_need_primary_key?
  false
end
default_key() click to toggle source

Default foreign key name symbol for foreign key in current model's table that points to the given association's table's primary key.

# File lib/sequel/model/associations.rb, line 816
def default_key
  :"#{self[:name]}_id"
end
eager_graph_lazy_dataset?() click to toggle source

Whether to eagerly graph a lazy dataset, true for many_to_one associations only if the key is nil.

# File lib/sequel/model/associations.rb, line 822
def eager_graph_lazy_dataset?
  self[:key].nil?
end
eager_graph_limit_strategy(_) click to toggle source

many_to_one associations don't need an eager_graph limit strategy

# File lib/sequel/model/associations.rb, line 827
def eager_graph_limit_strategy(_)
  nil
end
eager_limit_strategy() click to toggle source

many_to_one associations don't need an eager limit strategy

# File lib/sequel/model/associations.rb, line 832
def eager_limit_strategy
  nil
end
filter_by_associations_limit_strategy() click to toggle source

many_to_one associations don't need a filter by associations limit strategy

# File lib/sequel/model/associations.rb, line 837
def filter_by_associations_limit_strategy
  nil
end
finalize_settings() click to toggle source
# File lib/sequel/model/associations.rb, line 849
def finalize_settings
  FINALIZE_SETTINGS
end
predicate_key() click to toggle source

The expression to use on the left hand side of the IN lookup when eager loading

# File lib/sequel/model/associations.rb, line 854
def predicate_key
  cached_fetch(:predicate_key){qualified_primary_key}
end
primary_key() click to toggle source

The column(s) in the associated table that the key in the current table references (either a symbol or an array).

# File lib/sequel/model/associations.rb, line 859
def primary_key
 cached_fetch(:primary_key){associated_class.primary_key || raise(Error, "no primary key specified for #{associated_class.inspect}")}
end
primary_key_method() click to toggle source

The method symbol or array of method symbols to call on the associated object to get the value to use for the foreign keys.

# File lib/sequel/model/associations.rb, line 871
def primary_key_method
 cached_fetch(:primary_key_method){primary_key}
end
primary_key_methods() click to toggle source

The array of method symbols to call on the associated object to get the value to use for the foreign keys.

# File lib/sequel/model/associations.rb, line 877
def primary_key_methods
 cached_fetch(:primary_key_methods){Array(primary_key_method)}
end
primary_keys() click to toggle source

The columns in the associated table that the key in the current table references (always an array).

# File lib/sequel/model/associations.rb, line 864
def primary_keys
 cached_fetch(:primary_keys){Array(primary_key)}
end
Also aliased as: associated_object_keys
qualified_primary_key() click to toggle source

#primary_key qualified by the associated table

# File lib/sequel/model/associations.rb, line 882
def qualified_primary_key
  cached_fetch(:qualified_primary_key){self[:qualify] == false ? primary_key : qualify_assoc(primary_key)}
end
reciprocal_array?() click to toggle source

True only if the reciprocal is a one_to_many association.

# File lib/sequel/model/associations.rb, line 887
def reciprocal_array?
  !set_reciprocal_to_self?
end
returns_array?() click to toggle source

Whether this association returns an array of objects instead of a single object, false for a many_to_one association.

# File lib/sequel/model/associations.rb, line 893
def returns_array?
  false
end
set_reciprocal_to_self?() click to toggle source

True only if the reciprocal is a one_to_one association.

# File lib/sequel/model/associations.rb, line 898
def set_reciprocal_to_self?
  reciprocal
  reciprocal_type == :one_to_one
end

Private Instance Methods

ambiguous_reciprocal_type?() click to toggle source

Reciprocals of many_to_one associations could be either one_to_many or one_to_one, and which is not known in advance.

# File lib/sequel/model/associations.rb, line 907
def ambiguous_reciprocal_type?
  true
end
filter_by_associations_conditions_associated_keys() click to toggle source
# File lib/sequel/model/associations.rb, line 911
def filter_by_associations_conditions_associated_keys
  qualify(associated_class.table_name, primary_keys)
end
filter_by_associations_conditions_key() click to toggle source
# File lib/sequel/model/associations.rb, line 915
def filter_by_associations_conditions_key
  qualify(self[:model].table_name, self[:key_column])
end
limit_to_single_row?() click to toggle source

many_to_one associations do not need to be limited to a single row if they explicitly do not have a key.

# File lib/sequel/model/associations.rb, line 921
def limit_to_single_row?
  super && self[:key]
end
possible_reciprocal_types() click to toggle source

The reciprocal type of a many_to_one association is either a one_to_many or a one_to_one association.

# File lib/sequel/model/associations.rb, line 931
def possible_reciprocal_types
  [:one_to_many, :one_to_one]
end
predicate_key_methods() click to toggle source
# File lib/sequel/model/associations.rb, line 925
def predicate_key_methods
  self[:keys]
end
reciprocal_association?(assoc_reflect) click to toggle source

Whether the given association reflection is possible reciprocal

# File lib/sequel/model/associations.rb, line 936
def reciprocal_association?(assoc_reflect)
  super && self[:keys] == assoc_reflect[:keys] && primary_key == assoc_reflect.primary_key
end
reciprocal_type() click to toggle source

The reciprocal type of a many_to_one association is either a one_to_many or a one_to_one association, look in the associated class to try to figure out which.

# File lib/sequel/model/associations.rb, line 943
def reciprocal_type
  cached_fetch(:reciprocal_type) do
    possible_recips = []

    associated_class.all_association_reflections.each do |assoc_reflect|
      if reciprocal_association?(assoc_reflect)
        possible_recips << assoc_reflect
      end
    end

    if possible_recips.length == 1
      possible_recips.first[:type]
    else
      possible_reciprocal_types
    end
  end
end