# File lib/og/meta.rb, line 206
        def many_to_many(name, klass, options = {})
                list_o = name.to_s
                prop_o = N::Inflector.singularize(list_o)
                list_m = options[:linkback] || N::Inflector.plural_name(self) 
                prop_m = N::Inflector.singularize(list_m)
        
                # exit if the class is allready indirectly 'enchanted' from the
                # other class of the many_to_many relation.

                return if self.respond_to?(prop_m)
        
                # Add some metadata to the class to allow for automatic join table
                # calculation.

                meta :sql_join, [klass, options]

                meta :many_to_many, klass
                klass.meta :many_to_many, self 

                # enchant this class
                
                module_eval %{
                        def #{list_o}(extrasql = nil)
                                Og.db.select("SELECT d.* FROM #{Backend.table(klass)} AS d, #{Backend.join_table(self, klass)} AS j WHERE j.key1=\#\@oid AND j.key2=d.oid \#\{extrasql\}", #{klass})
                        end

                        def #{list_o}_count(extrasql = nil)
                                Og.db.select("SELECT COUNT(*) FROM #{Backend.table(klass)} AS d, #{Backend.join_table(self, klass)} AS j WHERE j.key1=\#\@oid AND j.key2=d.oid \#\{extrasql\}", #{klass})
                        end                                          
                        
                        def add_#{prop_o}(obj, extra = nil)
                                Og.db.exec("INSERT INTO #{Backend.join_table(self, klass)} (key1, key2) VALUES (\#\@oid, \#\{obj.oid\})") 
                        end
                        
                        def delete_#{prop_o}(obj_or_oid, extra = nil)
                                Og.db.exec("DELETE FROM #{Backend.join_table(self, klass)} WHERE key2=\#\{obj_or_oid.to_i\}") 
                        end

                        def clear_#{list_o}
                                Og.db.exec("DELETE FROM #{Backend.join_table(self, klass)} WHERE key1=\#\@oid") 
                        end                  
                }
                
                # indirectly enchant the other class of the relation.
                
                klass.module_eval %{
                        def #{list_m}(extrasql = nil)
                                Og.db.select("SELECT s.* FROM #{Backend.table(self)} AS s, #{Backend.join_table(self, klass)} AS j WHERE j.key2=\#\@oid AND j.key1=s.oid \#\{extrasql\}", #{self})
                        end

                        def #{list_m}_count(extrasql = nil)
                                Og.db.select("SELECT COUNT(*) FROM #{Backend.table(self)} AS s, #{Backend.join_table(self, klass)} AS j WHERE j.key2=\#\@oid AND j.key1=s.oid \#\{extrasql\}", #{self})
                        end
                        
                        def add_#{prop_m}(obj, extra = nil)
                                Og.db.exec("INSERT INTO #{Backend.join_table(self, klass)} (key1, key2) VALUES (\#\{obj.oid\}, \#\@oid)") 
                        end
                        
                        def delete_#{prop_m}(obj_or_oid, extra = nil)
                                Og.db.exec("DELETE FROM #{Backend.join_table(self, klass)} WHERE key1=\#\{obj_or_oid.to_i\}") 
                        end

                        def clear_#{list_m}
                                Og.db.exec("DELETE FROM #{Backend.join_table(self, klass)} WHERE key2=\#\@oid") 
                        end                  
                }
        end