Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created January 16, 2013 15:16
Show Gist options
  • Select an option

  • Save botanicus/4547857 to your computer and use it in GitHub Desktop.

Select an option

Save botanicus/4547857 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require "pry"
require "dm-core"
require "dm-migrations"
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, "mysql://localhost/test")
DataMapper::Model.raise_on_save_failure = true
class Tag
include DataMapper::Resource
property :name, String#, required: true, length: 1..20
property :slug, String, key: true#, auto_validation: false, unique: true, writer: :private
# Set automatically on create, don't change.
before(:create) do |tag|
tag.attribute_set(:slug, self.class.generate_slug(tag.name))
end
def self.generate_slug(name)
name.downcase.gsub(/\s+/, "-")
end
end
class Tagging
include DataMapper::Resource
belongs_to :tag, key: true
belongs_to :idea, key: true
end
class Idea
include DataMapper::Resource
property :id, Serial
property :name, String#, length: 4..35
# These are skills.
#
# ManyToMany#Relationship#through:
# {:name=>:tags_provided}
# {:through_model=>IdeaTag} # WTF? I expect Tagging
has n, :tags_provided, Tag, through: :taggings, via: :idea
has n, :tags_required, Tag, through: :taggings, via: :idea
end
DataMapper.auto_migrate!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment