Skip to content

Instantly share code, notes, and snippets.

@ohammersmith
Created June 2, 2009 21:40
Show Gist options
  • Save ohammersmith/122613 to your computer and use it in GitHub Desktop.
Save ohammersmith/122613 to your computer and use it in GitHub Desktop.
#!/opt/local/bin/ruby
require 'rubygems'
gem 'datamapper'
require 'datamapper'
DataMapper::Logger.new(STDOUT, :debug) # :off, :fatal, :error, :warn, :info, :debug
DataMapper.setup(:default, 'mysql://root:mXtHWdZsZuqh@localhost/dm_core_test' )
class Post
include DataMapper::Resource
# You can specify :serial (auto-incrementing, also sets :key to true):
property :id, Integer, :serial => true
# # ...or the Serial custom-type which is functionally identical to the above:
# property :id, Serial
# # ...or pass a :key option for a user-set key like the name of a user:
property :name, String, :key => true
#
# property :title, String
# property :body, Text
# property :created_at, DateTime
belongs_to :category
end
class Category
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :posts
end
DataMapper.auto_migrate!
category = Category.create(:name => "some awesome thing")
Post.create(:name => "foobar", :category => category)
puts "hi there, i'm done!!"
puts "post count for category #{category.posts.aggregate(:all.count, :name.count)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment