class MongoModel::Associations::HasManyByForeignKey

Public Instance Methods

define!() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 12
def define!
  raise "has_many :by => :foreign_key is only valid on Document" unless owner.ancestors.include?(Document)

  super

  define_dependency_callbacks!
  self
end
define_dependency_callbacks!() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 21
def define_dependency_callbacks!
  association = self

  if options[:dependent] == :destroy
    owner.before_destroy do
      send(association.name).each { |child| child.destroy }
    end
  elsif options[:dependent] == :delete
    owner.before_destroy do
      send(association.name).delete_all
    end
  end
end
foreign_key() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 4
def foreign_key
  @foreign_key ||= options[:foreign_key] || :"#{inverse_of}_id"
end
inverse_of() click to toggle source
# File lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb, line 8
def inverse_of
  @inverse_of ||= options[:inverse_of] || owner.to_s.demodulize.underscore.singularize.to_sym
end