A Datamapper is remixable example

I struggled a bit this afternoon getting Datamapper’s dm-is-remixable plug-in as there aren’t too many posts out there and some are out of date. Hopefully this quick overview will spare you my pain :)

Many sites allow you to comment on blog posts, images, videos, status, etc. having one comments table with post_id, image_id, etc. as with a One-to-Many relationship leads to a giant table of unrelated (other than by user_id) data. In my opinion it’s better to have a table for each e.g. post_comments, image_comments, etc. Enter dm-is-remixable.

Step 1: Install

We’ll install dm-is-remixable and friends. Open up a terminal and enter:

sudo gem install do_sqlite3
sudo gem install datamapper
sudo gem install dm-migrations
sudo gem install dm-is-remixable

Step 2: Setup

Create a new file called remixable.rb we’ll user this file from now on. First enter the required gems and set Datamapper’s log and sqlite file.

require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-is-remixable'

# setup

DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/remixable.db")

Step 3 Define the Models

We’ll create four models, one module and three classes. User, Post, and Image are classes, and Comment will be a module. The relationships are a user has many posts and images, posts and images belong to a user, nothing new yet, and post and image remix many comments for a user. What? This is the same as saying a post has many comments and said comment(s) belong to a user.

# module and classes

module Comment
  include DataMapper::Resource

  property :id, Serial
  property :comment, Text

  is :remixable
end

class User
  include DataMapper::Resource

  property :id, Serial
  property :username, String

  has n, :images
  has n, :posts
end

class Post
  include DataMapper::Resource

  property :id, Serial
  property :title, String
  property :body, Text

  belongs_to :user
  remix n, :comments, :for => 'User'
end

class Image
  include DataMapper::Resource

  property :id, Serial
  property :path, String

  belongs_to :user
  remix n, :comments, :for => 'User'
end

Step 4: Migrate

Next we’ll finalize our models and run auto_migrate to create the tables. During this step Datamapper, via is remixable, will generate two anonymous model classes PostComment and ImageComment. Five tables will be created users, posts, images, post_comments, and image_comments.

# lock and load

DataMapper.finalize
DataMapper.auto_migrate!

Step 5: Do Something

The last step is to create some dummy data to test out our models. We’ll create two users ‘foo’ and ‘funk’, foo will create post and ‘image’ and ‘funk’ will comment on them. The last two lines show how to access post and image comments for a user.

# go!

foo = User.create(
  :username => "foo"
)

funk = User.create(
  :username => "funk"
)

post = Post.create(
  :user_id => foo.id,
  :title => "My great post",
  :body => "This is my great post"
)

post_comment = PostComment.create(
  :user_id => funk.id,
  :post_id => post.id,
  :comment => "ballz I say!"
)

image = Image.create(
  :user_id => foo.id,
  :path => "/some/image/path"
)

image_comment = ImageComment.create(
  :user_id => funk.id,
  :image_id => image.id,
  :comment => "you look funny!"
)

puts funk.post_comments[0].comment
puts funk.image_comments[0].comment

That’s it!

Post a Comment

Your email is never shared. Required fields are marked *

*
*

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word