Class: Mongoid::Associations::HasManyRelated

Inherits:
Proxy show all
Defined in:
lib/mongoid/associations/has_many_related.rb

Overview

Represents an relational one-to-many association with an object in a separate collection or database.

Instance Attribute Summary

Attributes inherited from Proxy

#options, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Proxy

#extends

Constructor Details

#initialize(document, options, target = nil) ⇒ HasManyRelated

Initializing a related association only requires looking up the objects by their ids.

Options:

document: The Document that contains the relationship. options: The association Options.



97
98
99
100
101
102
103
# File 'lib/mongoid/associations/has_many_related.rb', line 97

def initialize(document, options, target = nil)
  @parent, @klass, @options = document, options.klass, options
  @foreign_key = options.foreign_key
  @base = lambda { @klass.all(:conditions => { @foreign_key => document.id }) }
  @target = target || @base.call
  extends(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Override the default behavior to allow the criteria to get reset on each call into the association.

Example:

person.posts.where(:title => "New")
person.posts # resets the criteria

Returns:

A Criteria object or Array.



116
117
118
119
# File 'lib/mongoid/associations/has_many_related.rb', line 116

def method_missing(name, *args, &block)
  @target = @base.call unless @target.is_a?(Array)
  @target.send(name, *args, &block)
end

Class Method Details

.instantiate(document, options, target = nil) ⇒ Object

Preferred method for creating the new HasManyRelated association.

Options:

document: The Document that contains the relationship. options: The association Options.



151
152
153
# File 'lib/mongoid/associations/has_many_related.rb', line 151

def instantiate(document, options, target = nil)
  new(document, options, target)
end

.macroObject

Returns the macro used to create the association.



156
157
158
# File 'lib/mongoid/associations/has_many_related.rb', line 156

def macro
  :has_many_related
end

.update(target, document, options) ⇒ Object

Perform an update of the relationship of the parent and child. This will assimilate the child Document into the parent’s object graph.

Options:

related: The related object parent: The parent Document to update. options: The association Options

Example:

RelatesToOne.update(game, person, options)



172
173
174
175
176
# File 'lib/mongoid/associations/has_many_related.rb', line 172

def update(target, document, options)
  name = document.class.to_s.underscore
  target.each { |child| child.send("#{name}=", document) }
  instantiate(document, options, target)
end

Instance Method Details

#<<(*objects) ⇒ Object

Appends the object to the Array, setting its parent in the process.



10
11
12
13
14
15
16
17
# File 'lib/mongoid/associations/has_many_related.rb', line 10

def <<(*objects)
  load_target
  objects.flatten.each do |object|
    object.send("#{@foreign_key}=", @parent.id)
    @target << object
    object.save unless @parent.new_record?
  end
end

#build(attributes = {}) ⇒ Object

Builds a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor.

Returns the newly created object.



24
25
26
27
28
29
30
# File 'lib/mongoid/associations/has_many_related.rb', line 24

def build(attributes = {})
  load_target
  name = @parent.class.to_s.underscore
  object = @klass.instantiate(attributes.merge(name => @parent))
  @target << object
  object
end

#concat(*objects) ⇒ Object

Delegates to <<



33
34
35
# File 'lib/mongoid/associations/has_many_related.rb', line 33

def concat(*objects)
  self << objects
end

#create(attributes) ⇒ Object

Creates a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor and the new object will then be saved.

Returns the newly created object.



43
44
45
46
# File 'lib/mongoid/associations/has_many_related.rb', line 43

def create(attributes)
  object = build(attributes)
  object.save; object
end

#create!(attributes) ⇒ Object

Creates a new Document and adds it to the association collection. If validation fails an error is raised.

Returns the newly created object.



52
53
54
55
# File 'lib/mongoid/associations/has_many_related.rb', line 52

def create!(attributes)
  object = build(attributes)
  object.save!; object
end

#delete_all(conditions = {}) ⇒ Object

Delete all the associated objects.

Example:

person.posts.delete_all

Returns:

The number of objects deleted.



66
67
68
# File 'lib/mongoid/associations/has_many_related.rb', line 66

def delete_all(conditions = {})
  remove(:delete_all, conditions[:conditions])
end

#destroy_all(conditions = {}) ⇒ Object

Destroy all the associated objects.

Example:

person.posts.destroy_all

Returns:

The number of objects destroyed.



79
80
81
# File 'lib/mongoid/associations/has_many_related.rb', line 79

def destroy_all(conditions = {})
  remove(:destroy_all, conditions[:conditions])
end

#find(*args) ⇒ Object

Finds a document in this association. If an id is passed, will return the document for that id.



85
86
87
88
# File 'lib/mongoid/associations/has_many_related.rb', line 85

def find(*args)
  args[1][:conditions].merge!(@foreign_key.to_sym => @parent.id) if args.size > 1
  @klass.find(*args)
end

#push(*objects) ⇒ Object

Delegates to <<



122
123
124
# File 'lib/mongoid/associations/has_many_related.rb', line 122

def push(*objects)
  self << objects
end