class MongoModel::Associations::HasManyByForeignKey::Association

Public Instance Methods

assign(doc) click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 75
def assign(doc)
  set_inverse_association(doc)
  doc.save(false) unless doc.new_record?
end
build(*args, &block) click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 49
def build(*args, &block)
  doc = scoped.new(*args, &block)
  new_documents << doc
  doc
end
create(*args) { |doc| ... } click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 55
def create(*args)
  scoped.create(*args) do |doc|
    set_inverse_association(doc)
    yield doc if block_given?
  end
end
create!(*args) { |doc| ... } click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 62
def create!(*args)
  scoped.create!(*args) do |doc|
    set_inverse_association(doc)
    yield doc if block_given?
  end
end
ensure_class(array) click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 94
def ensure_class(array)
  array.is_a?(Array) ? array.each { |i| super(i) } : super
end
find_target() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 43
def find_target
  scoped.each { |doc|
    doc.send("#{inverse_of}=", instance) if doc.respond_to?("#{inverse_of}=")
  } + new_documents
end
replace(array) click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 69
def replace(array)
  ensure_class(array)
  array.each { |doc| assign(doc) }
  super
end
scoped() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 90
def scoped
  definition.scope.where(foreign_key => instance.id).on_load { |doc| set_inverse_association(doc) }
end
unset(doc) click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 80
def unset(doc)
  if doc.respond_to?("#{inverse_of}=")
    doc.send("#{inverse_of}=", nil) if doc.send(inverse_of) == instance
  else
    doc[foreign_key] = nil if doc[foreign_key] == instance.id
  end

  doc.save(false) unless doc.new_record?
end

Protected Instance Methods

new_documents() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 99
def new_documents
  @new_documents ||= []
end
proxy_class() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 111
def proxy_class
  Proxy
end
set_inverse_association(doc) click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 103
def set_inverse_association(doc)
  if doc.respond_to?("#{inverse_of}=")
    doc.send("#{inverse_of}=", instance)
  else
    doc[foreign_key] = instance.id
  end
end