Module: MongoMapper::Associations::ClassMethods

Defined in:
lib/mongomapper/associations.rb

Instance Method Summary collapse

Instance Method Details

#associationsObject



35
36
37
# File 'lib/mongomapper/associations.rb', line 35

def associations
  @associations ||= HashWithIndifferentAccess.new
end

#belongs_to(association_id, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongomapper/associations.rb', line 4

def belongs_to(association_id, options = {})
  association = create_association(:belongs_to, association_id, options)

  ref_id = "#{association_id}_id"
  key ref_id, String

  define_method("#{ref_id}=") do |value|
    write_attribute(ref_id, value)
  end

  if options[:polymorphic]
    ref_type = "#{association_id}_type"
    key ref_type, String

    define_method("#{ref_type}=") do |value|
      write_attribute(ref_type, value)
    end
  end

  define_association_methods(association)

  self
end

#many(association_id, options = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/mongomapper/associations.rb', line 28

def many(association_id, options = {})
  association = create_association(:many, association_id, options)
  define_association_methods(association)

  self
end