ruby/sequel/polymorphic.rb
# Creates a many-to-many relationship. NB: Removing/clearing the collection deletes the instances in the through relationship (as opposed to nullifying the *able fields as in the one-to-many)
# Example: Post.many_to_many :tags, :through => :taggings, :as => :taggable
Creates a many-to-many relationship.
Creates a many - to - many relationship .
def many_to_many(*args, &block)
          collection_name, options = *args
          options ||= {}
          if through = (options[:through] || options[:join_table]) and able = options[:as]
            able_id                = :"#{able}_id"
            able_type              = :"#{able}_type"
            collection_singular    = singularize(collection_name.to_s).to_sym 
            collection_singular_id = :"#{collection_singular}_id"
            through_klass          = constantize(singularize(through.to_s).capitalize) 
            associate(:many_to_many, collection_name,
              :left_key   => able_id,
              :join_table => through,
              :conditions => {able_type => self.to_s.downcase},
              :adder      => proc { |many_of_instance| through_klass.create(collection_singular_id => many_of_instance.pk, able_id => pk, able_type => self.class.to_s) },
              :remover    => proc { |many_of_instance| through_klass.where(collection_singular_id => many_of_instance.pk, able_id => pk, able_type => self.class.to_s).delete },
              :clearer    => proc { through_klass.where(able_id => pk, able_type => self.class.to_s).delete }
            )
          else
            associate(:many_to_many, *args, &block)
          end
        end
def many_to_many ( * args , & block ) collection_name , options = * args options ||= { } if through = ( options [ :through ] || options [ :join_table ] ) and able = options [ :as ] able_id = :" #{ able } _id " able_type = :" #{ able } _type " collection_singular = singularize ( collection_name . to_s ) . to_sym collection_singular_id = :" #{ collection_singular } _id " through_klass = constantize ( singularize ( through . to_s ) . capitalize ) associate ( :many_to_many , collection_name , :left_key => able_id , :join_table => through , :conditions => { able_type => self . to_s . downcase } , :adder => proc { | many_of_instance | through_klass . create ( collection_singular_id => many_of_instance . pk , able_id => pk , able_type => self . class . to_s ) } , :remover => proc { | many_of_instance | through_klass . where ( collection_singular_id => many_of_instance . pk , able_id => pk , able_type => self . class . to_s ) . delete } , :clearer => proc { through_klass . where ( able_id => pk , able_type => self . class . to_s ) . delete } ) else associate ( :many_to_many , * args , & block ) end end
Creates a many-to-many relationship.
Creates a many - to - many relationship .
# => tag # => Tagging
args
block