Created
August 16, 2011 13:36
-
-
Save deplorableword/1149081 to your computer and use it in GitHub Desktop.
datamapper-associations.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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