<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alastair&#039;s Axioms &#187; remixable</title>
	<atom:link href="http://blog.alastairdawson.com/tag/remixable/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.alastairdawson.com</link>
	<description>Let us toast your non-idiocy</description>
	<lastBuildDate>Thu, 11 Aug 2011 23:13:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Datamapper is remixable example</title>
		<link>http://blog.alastairdawson.com/2010/07/29/a-datamapper-is-remixable-example/</link>
		<comments>http://blog.alastairdawson.com/2010/07/29/a-datamapper-is-remixable-example/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 01:10:23 +0000</pubDate>
		<dc:creator>Alastair</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[datamapper]]></category>
		<category><![CDATA[dm-is-remixable]]></category>
		<category><![CDATA[remixable]]></category>
		<category><![CDATA[sinatra]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://blog.alastairdawson.com/?p=219</guid>
		<description><![CDATA[I struggled a bit this afternoon getting Datamapper&#8217;s dm-is-remixable plug-in as there aren&#8217;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. [...]]]></description>
			<content:encoded><![CDATA[<p>I struggled a bit this afternoon getting <a href="http://datamapper.org/">Datamapper&#8217;s</a> dm-is-remixable plug-in as there aren&#8217;t too many posts out there and some are out of date. Hopefully this quick overview will spare you my pain <img src='http://blog.alastairdawson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>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&#8217;s better to have a table for each e.g. post_comments, image_comments, etc. Enter dm-is-remixable.</p>
<p><strong>Step 1: Install</strong></p>
<p>We&#8217;ll install dm-is-remixable and friends. Open up a terminal and enter:</p>
<pre>
sudo gem install do_sqlite3
sudo gem install datamapper
sudo gem install dm-migrations
sudo gem install dm-is-remixable
</pre>
<p><strong>Step 2: Setup</strong></p>
<p>Create a new file called remixable.rb we&#8217;ll user this file from now on. First enter the required gems and set Datamapper&#8217;s log and sqlite file.</p>
<pre>
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")
</pre>
<p><strong>Step 3 Define the Models</strong></p>
<p>We&#8217;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. </p>
<pre>
# 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
</pre>
<p><strong>Step 4: Migrate</strong></p>
<p>Next we&#8217;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.</p>
<pre>
# lock and load

DataMapper.finalize
DataMapper.auto_migrate!
</pre>
<p><strong>Step 5: Do Something</strong></p>
<p>The last step is to create some dummy data to test out our models. We&#8217;ll create two users &#8216;foo&#8217; and &#8216;funk&#8217;, foo will create post and &#8216;image&#8217; and &#8216;funk&#8217; will comment on them. The last two lines show how to access post and image comments for a user.</p>
<pre>
# 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
</pre>
<p>That&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alastairdawson.com/2010/07/29/a-datamapper-is-remixable-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

