Skip to content

Instantly share code, notes, and snippets.

@deplorableword
Created August 16, 2011 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deplorableword/1149081 to your computer and use it in GitHub Desktop.
Save deplorableword/1149081 to your computer and use it in GitHub Desktop.
datamapper-associations.rb
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/todo.db")
class Tag
include DataMapper::Resource
property :id, Serial
property :title, String
belongs_to :todo
end
class Todo
include DataMapper::Resource
property :id, Serial
property :title, String
has n, :tag
end
DataMapper.auto_migrate!
get '/' do
# create and save a new todo
@newtodo = Todo.create(:title => 'washing up')
@newtodo.save
# find the todo by title
@todo = Todo.first(:title => 'washing up')
# add some tags to it
@todo.tag.create(:title => 'hassle')
@todo.tag.create(:title => 'boring')
# list everything
output = " "
Todo.all.each do |t|
output << "Title: #{t.title} <br />"
output << "tags: "
t.tag.all.each do |thetag|
output << " #{thetag.title}, "
end
end
return output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment