Class: Mongoid::Association::Referenced::HasOne

Inherits:
Object
  • Object
show all
Includes:
Buildable, Mongoid::Association::Relatable
Defined in:
build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/eager.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/proxy.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/binding.rb,
build/mongoid-7.3/lib/mongoid/association/referenced/has_one/buildable.rb

Overview

The has_one association.

Since:

  • 7.0

Defined Under Namespace

Modules: Buildable Classes: Binding, Eager, Proxy

Constant Summary collapse

ASSOCIATION_OPTIONS =

The options available for this type of association, in addition to the common ones.

Returns:

  • (Array<Symbol>)

    The extra valid options.

Since:

  • 7.0

[
    :as,
    :autobuild,
    :autosave,
    :dependent,
    :foreign_key,
    :primary_key
].freeze
VALID_OPTIONS =

The complete list of valid options for this association, including the shared ones.

Returns:

  • (Array<Symbol>)

    The valid options.

Since:

  • 7.0

(ASSOCIATION_OPTIONS + SHARED_OPTIONS).freeze
FOREIGN_KEY_SUFFIX =

The default foreign key suffix.

Returns:

  • (String)

    ‘_id’

Since:

  • 7.0

'_id'.freeze

Constants included from Mongoid::Association::Relatable

Mongoid::Association::Relatable::PRIMARY_KEY_DEFAULT, Mongoid::Association::Relatable::SHARED_OPTIONS

Instance Attribute Summary

Attributes included from Mongoid::Association::Relatable

#name, #options

Instance Method Summary collapse

Methods included from Buildable

#build

Methods included from Mongoid::Association::Relatable

#==, #counter_cache_column_name, #create_relation, #destructive?, #extension, #foreign_key_check, #foreign_key_setter, #get_callbacks, #initialize, #inverse, #inverse_association, #inverse_class, #inverse_class_name, #inverse_setter, #inverse_type, #inverse_type_setter, #inverses, #key, #relation_class, #relation_class_name, #setter, #type_setter, #validate?

Methods included from Options

#as, #autobuilding?, #autosave, #cascading_callbacks?, #counter_cached?, #cyclic?, #dependent, #forced_nil_inverse?, #indexed?, #inverse_of, #order, #primary_key, #store_as, #touch_field

Methods included from Constrainable

#convert_to_foreign_key

Instance Method Details

#bindable?(doc) ⇒ true, false

Whether trying to bind an object using this association should raise an error.

Parameters:

  • doc (Document)

    The document to be bound.

Returns:

  • (true, false)

    Whether the document can be bound.

Since:

  • 7.0



141
142
143
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 141

def bindable?(doc)
  forced_nil_inverse? || (!!inverse && doc.fields.keys.include?(foreign_key))
end

#embedded?false

Is this association type embedded?

Returns:

  • (false)

    Always false.

Since:

  • 7.0



85
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 85

def embedded?; false; end

#foreign_keyString

Get the foreign key field for saving the association reference.

Returns:

  • (String)

    The foreign key field for saving the association reference.

Since:

  • 7.0



75
76
77
78
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 75

def foreign_key
  @foreign_key ||= @options[:foreign_key] ? @options[:foreign_key].to_s :
                     default_foreign_key_field
end

#nested_builder(attributes, options) ⇒ Association::Nested::Many

The nested builder object.

Parameters:

  • attributes (Hash)

    The attributes to use to build the association object.

  • options (Hash)

    The options for the association.

Returns:

Since:

  • 7.0



111
112
113
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 111

def nested_builder(attributes, options)
  Nested::One.new(self, attributes, options)
end

#path(document) ⇒ Root

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.

Since:

  • 2.1.0



157
158
159
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 157

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end

#polymorphic?true, false

Is this association polymorphic?

Returns:

  • (true, false)

    Whether this association is polymorphic.

Since:

  • 7.0



120
121
122
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 120

def polymorphic?
  @polymorphic ||= !!as
end

#relationAssociation::HasOne::Proxy

Get the association proxy class for this association type.

Returns:

  • (Association::HasOne::Proxy)

    The proxy class.

Since:

  • 7.0



99
100
101
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 99

def relation
  Proxy
end

#relation_complementsArray<Association>

The list of association complements.

Returns:

  • (Array<Association>)

    The association complements.

Since:

  • 7.0



55
56
57
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 55

def relation_complements
  @relation_complements ||= [ Referenced::BelongsTo ].freeze
end

#setup!self

Setup the instance methods, fields, etc. on the association owning class.

Returns:

  • (self)

Since:

  • 7.0



64
65
66
67
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 64

def setup!
  setup_instance_methods!
  self
end

#stores_foreign_key?Boolean

Returns:

Since:

  • 7.0



145
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 145

def stores_foreign_key?; false; end

#typeString?

Note:

Only relevant for polymorphic associations.

The type of this association if it’s polymorphic.

Returns:

  • (String, nil)

    The type field.

Since:

  • 7.0



131
132
133
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 131

def type
  @type ||= "#{as}_type" if polymorphic?
end

#validation_defaulttrue

The default for validation the association object.

Returns:

  • (true)

    Always true.

Since:

  • 7.0



92
# File 'build/mongoid-7.3/lib/mongoid/association/referenced/has_one.rb', line 92

def validation_default; true; end