A ‘has_many’ relation. There should be a respective ‘belongs_to’ relation.

Examples

article.comments << Comment.new article.comments.size

Methods
Public Instance methods
enchant()
     # File lib/og/relation/has_many.rb, line 31
 31:   def enchant
 32:     self[:owner_singular_name] = if owner_class.schema_inheritance?
 33:       owner_class.schema_inheritance_root_class
 34:     else
 35:       owner_class
 36:     end.to_s.demodulize.underscore.downcase
 37:     
 38:     self[:target_singular_name] = target_plural_name.to_s.singular
 39:     self[:foreign_key] = "#{foreign_name || owner_singular_name}_#{owner_class.primary_key}"
 40:     # gmosx: DON'T set self[:foreign_field]
 41:     foreign_field = self[:foreign_field] || self[:foreign_key]
 42:     
 43:     owner_class.module_eval %{ 
 44:       attr_accessor :#{target_plural_name}
 45: 
 46:       def #{target_plural_name}(options = nil)
 47:         unless @#{target_plural_name}
 48:           @#{target_plural_name} = HasManyCollection.new(
 49:             self, 
 50:             #{target_class},
 51:             :add_#{target_singular_name},
 52:             :remove_#{target_singular_name},
 53:             :find_#{target_plural_name},
 54:             :count_#{target_plural_name},
 55:             options
 56:           )
 57:         end
 58:         
 59:         @#{target_plural_name}.find_options = options
 60:         @#{target_plural_name}.reload(options) if options and options[:reload]
 61:         @#{target_plural_name}
 62:       end
 63:       
 64:       def #{target_plural_name}=(*args)
 65:         options = args.last.is_a?(Hash) ? args.pop : nil
 66:         
 67:         if args.size == 1 && args.first.is_a?(HasManyCollection)
 68:           @#{target_plural_name} = args.first
 69:           @#{target_plural_name}.find_options = options if options
 70:           
 71:           return @#{target_plural_name}
 72:         end
 73:         
 74:         args.flatten!
 75:         
 76:         args.each do |obj|
 77:           add_#{target_singular_name}(obj, options)
 78:         end
 79:         
 80:         return #{target_plural_name}
 81:       end
 82: 
 83:       def add_#{target_singular_name}(obj, options = nil)
 84:         return unless obj
 85:         # save the object if needed to generate a primary_key.
 86:         self.save unless self.saved?
 87:         obj.#{foreign_key} = @#{owner_class.primary_key}
 88:         obj.save
 89:       end
 90: 
 91:       def remove_#{target_singular_name}(obj)
 92:         obj.#{foreign_key} = nil
 93:         obj.save
 94:       end
 95:       
 96:       def find_#{target_plural_name}(options = {})
 97:         find_options = {
 98:           :condition => "#{foreign_field} = \#{og_quote(#{owner_class.primary_key})}"
 99:         }
100:         if options
101:           if condition = options.delete(:condition)
102:             find_options[:condition] += " AND (\#{condition})"
103:           end        
104:           find_options.update(options)
105:         end
106:         #{target_class}.find(find_options)
107:       end
108:       
109:       def count_#{target_plural_name}
110:         #{target_class}.count(:condition => "#{foreign_field} = \#{og_quote(#{owner_class.primary_key})}")        
111:       end
112:     }
113:   end
resolve_polymorphic()
    # File lib/og/relation/has_many.rb, line 23
23:   def resolve_polymorphic
24:     unless target_class.relations.empty?
25:       unless target_class.relations.find { |r| r.is_a?(BelongsTo) and  r.target_class == owner_class }
26:         target_class.belongs_to(owner_class)      
27:       end
28:     end
29:   end