<?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>Dr Nic &#187; Composite Keys</title>
	<atom:link href="http://drnicwilliams.com/category/ruby/ruby-on-rails/composite-keys/feed/" rel="self" type="application/rss+xml" />
	<link>http://drnicwilliams.com</link>
	<description>Ruby makes Rails, Javascript makes Ajax, Dr Nic makes Magic</description>
	<lastBuildDate>Tue, 03 Aug 2010 23:44:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Composite Primary Keys goes 1.0.0 for Rails 2.1</title>
		<link>http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/</link>
		<comments>http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 03:29:49 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Composite Keys]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/?p=277</guid>
		<description><![CDATA[Two years ago Dave Thomas did a keynote at the first RailsConf in 2006 and outlined a few things missing in Rails. One was the seeming unnecessary un-DRYness of duplicating associations and validations in Active Record models since the same information is in the DB schema. Another was support for Composite Primary Keys on Active [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2009/10/06/install-any-html-themetemplate-into-your-rails-app/' rel='bookmark' title='Permanent Link: Install any HTML theme/template into your Rails app'>Install any HTML theme/template into your Rails app</a> <small> Have you ever even bothered to Google for &#8220;rails...</small></li><li><a href='http://drnicwilliams.com/2009/03/30/closing-in-on-the-dream-one-click-to-deploy-rails-apps/' rel='bookmark' title='Permanent Link: Closing in on The Dream: &#8220;one-click-to-deploy Rails apps&#8221;'>Closing in on The Dream: &#8220;one-click-to-deploy Rails apps&#8221;</a> <small> Got a simple app you want to build? Allocate...</small></li><li><a href='http://drnicwilliams.com/2007/07/26/sample-app-rails-multiple-openids-per-user/' rel='bookmark' title='Permanent Link: Sample Rails app: multi-OpenIDs per user'>Sample Rails app: multi-OpenIDs per user</a> <small>Last time, on &#8220;Dr Nic loves OpenID&#8221;&#8230; Dr Nic had...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Two years ago Dave Thomas did a keynote at the first RailsConf in 2006 and outlined a few things missing in Rails. One was the seeming unnecessary un-DRYness of duplicating associations and validations in Active Record models since the same information is in the DB schema. Another was support for Composite Primary Keys on Active Record models.</p>
<p>A few weeks later I created my first RubyGems as an attempt to solve these problems: <a href="http://magicmodels.rubyforge.org/dr_nic_magic_models/">Dr Nic&#8217;s Magic Models</a> and <a href="http://compositekeys.rubyforge.org/">Composite Primary Keys</a>. The former was funny and an entertaining way to use <code>const_missing?</code> and <code>Class.new</code>. The latter was not funny. It started at the heights of &#8220;mildly entertaining,&#8221; dropped down from there and then over the subsequent months it never again rose above the humour-scale heights of &#8220;please shoot me in the foot.&#8221;</p>
<p>There were entire months that I hated ever having created the Composite Primary Keys project <img src='http://drnicwilliams.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>Why? It sits precariously atop of ActiveRecord::Base, and a half dozen other ActiveRecord classes. It overrides methods, recreates entire blocks of SQL, and must work on all the different database adapters. That is, it is very sensitive to many changes in edge Rails. And refactoring ActiveRecord is a favourite pastime of the Rails Core team.</p>
<p>Unlike the Magic Models project which is perfectly useful for party tricks but probably partially pointless elsewhere, the CPK project just seemed to be so damned useful to some people. </p>
<p>Many people kept using it, several people contributed patches for adapter support and new features, and finally the Prophet arrived. The man that would lead the faithful forward to the Promised Land.</p>
<p><a href='http://drnicwilliams.com/wp-content/uploads/2008/06/sydney-opera-house-and-sydney-harbour-bridge-at-sunset-sydney-australia-posters.jpg'><img src="http://drnicwilliams.com/wp-content/uploads/2008/06/sydney-opera-house-and-sydney-harbour-bridge-at-sunset-sydney-australia-posters-225x300.jpg" alt="" title="sydney-opera-house-and-sydney-harbour-bridge-at-sunset-sydney-australia-posters" width="225" height="300" class="alignright size-medium wp-image-278" /></a></p>
<p>His name is Darrin &#8220;Champion&#8221; Holst. </p>
<p>Today, CPK was given the golden release number 1.0.0 and officially supports Rails 2.1 (but no longer supports 2.0.2 afaik). </p>
<p>CPK users might remember that we went <a href="http://rubyforge.org/frs/?group_id=1953&#038;release_id=22704">0.9.90 back in January</a>, and its now June. This led Darrin to comment in his <a href="http://groups.google.com/group/compositekeys/browse_thread/thread/648f675084f2e531">release email to the mailing list</a>:</p>
<blockquote><p>I&#8217;d like to name this release the &#8220;we&#8217;re running out of 0.9.x numbers, so it has to go to 1.0 sooner or later&#8221; release.</p></blockquote>
<p>Maintaining CPK is like being on the team that paints the Sydney Harbour Bridge &#8211; it takes then 12 mths to paint it from one end to the other, which is just in time to restart painting the bloody thing again.</p>
<p>So in every way that I (tried to) abandon the project, I am proud of Darrin for looking after it and all its users.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2009/10/06/install-any-html-themetemplate-into-your-rails-app/' rel='bookmark' title='Permanent Link: Install any HTML theme/template into your Rails app'>Install any HTML theme/template into your Rails app</a> <small> Have you ever even bothered to Google for &#8220;rails...</small></li><li><a href='http://drnicwilliams.com/2009/03/30/closing-in-on-the-dream-one-click-to-deploy-rails-apps/' rel='bookmark' title='Permanent Link: Closing in on The Dream: &#8220;one-click-to-deploy Rails apps&#8221;'>Closing in on The Dream: &#8220;one-click-to-deploy Rails apps&#8221;</a> <small> Got a simple app you want to build? Allocate...</small></li><li><a href='http://drnicwilliams.com/2007/07/26/sample-app-rails-multiple-openids-per-user/' rel='bookmark' title='Permanent Link: Sample Rails app: multi-OpenIDs per user'>Sample Rails app: multi-OpenIDs per user</a> <small>Last time, on &#8220;Dr Nic loves OpenID&#8221;&#8230; Dr Nic had...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>An Interview with Dr Nic</title>
		<link>http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/</link>
		<comments>http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 07:36:51 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Interviews]]></category>
		<category><![CDATA[Magic Models]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/</guid>
		<description><![CDATA[Do you want answers to questions like: What do you think about the current Twitter vs DHH discussion? How did you come by with the Magic Model solution? What does the New Gem Generator actually do? Will we see ‘Rails Distros’ in the long run? Where are you from? What’s your current day job, is [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/11/04/iphone-dev-podcast-about-fmdb-migration-manager-and-rbiphonetest/' rel='bookmark' title='Permanent Link: iPhone dev podcast about fmdb-migration-manager and rbiphonetest'>iPhone dev podcast about fmdb-migration-manager and rbiphonetest</a> <small>Radio is where ugly people go. Podcasts is where ugly,...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li><li><a href='http://drnicwilliams.com/2007/03/02/5-things-im-in-love-with/' rel='bookmark' title='Permanent Link: 5 things I&#8217;m in love with'>5 things I&#8217;m in love with</a> <small>In no specific order, but enumerated for good measure: autotest...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Do you want answers to questions like:</p>
<ul>
<li>What do you think about the current Twitter vs DHH discussion?</li>
<li>How did you come by with the <a href="http://magicmodels.rubyforge.org/">Magic Model</a> solution?</li>
<li>What does the <a href="http://newgem.rubyforge.org/">New Gem Generator</a> actually do?</li>
<li>Will we see ‘Rails Distros’ in the long run?</li>
<li>Where are you from? What’s your current day job, is it Rails related? And what’s your history with Rails?</li>
<li>Did you get your free t-shirt?</li>
</ul>
<p>Brazilian <a href="http://www.akitaonrails.com/">Fabio Akita</a> &#8211; author of the first <a href="http://www.brasport.com.br/index.php?Escolha=8&#038;Livro=L00209">Brazilian book on Ruby on Rails</a> also wanted to know, <a href="http://www.akitaonrails.com/pages/drnic">so he asked</a>.</p>
<p>Thanks to my friend <a href="http://www.egenial.com.br/">Carlos Eduardo</a> for teeing it up.</p>
<h1><a href="http://www.akitaonrails.com/pages/drnic">Go to interview</a></h1>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/11/04/iphone-dev-podcast-about-fmdb-migration-manager-and-rbiphonetest/' rel='bookmark' title='Permanent Link: iPhone dev podcast about fmdb-migration-manager and rbiphonetest'>iPhone dev podcast about fmdb-migration-manager and rbiphonetest</a> <small>Radio is where ugly people go. Podcasts is where ugly,...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li><li><a href='http://drnicwilliams.com/2007/03/02/5-things-im-in-love-with/' rel='bookmark' title='Permanent Link: 5 things I&#8217;m in love with'>5 things I&#8217;m in love with</a> <small>In no specific order, but enumerated for good measure: autotest...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/04/17/an-interview-with-dr-nic/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Magic Models: the Spring collection</title>
		<link>http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/</link>
		<comments>http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 06:44:08 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Magic Models]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/</guid>
		<description><![CDATA[Over the last few months, we at Dr Nic Magic Models (Sweden) Inc. have been gradually selling down all the company shares to pension funds at over-inflated prices, whilst at the same time refusing to release new patches for old projects that sorely need them &#8211; Composite Primary Keys and Dr Nic&#8217;s Magic Models. Magic [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Over the last few months, we at <em>Dr Nic Magic Models (Sweden) Inc.</em> have been gradually selling down all the company shares to pension funds at over-inflated prices, whilst at the same time refusing to release new patches for old projects that sorely need them &#8211; Composite Primary Keys and Dr Nic&#8217;s Magic Models. </p>
<p>Magic Models 0.8.0 exhibited range of wacky behaviour and I&#8217;ve been suggesting people 0.7.2 for a long time, instead of releasing 0.9.0, which I mostly wrote last year.</p>
<p>I received a huge patch for Composite Primary Keys in my inbox in February and despite the emails of &#8220;<a href="http://groups.google.com/group/compositekeys/browse_thread/thread/48dc839c7fce730e">when will this patch be released</a>&#8221; by the punters, I sat on it.</p>
<p>I&#8217;ve also mentioned in emails several new projects that I&#8217;ve either never released or they are lurking on RubyForge without any celebratory fanfare. For example, the <strong>Magic Model Generator</strong> &#8211; got 200 tables in a legacy schema? Generate 200 model files in 20 seconds.</p>
<p>The share price has now plummeted, and over Easter I bought back all the shares at half price. All that&#8217;s left to do now is unveil a barrage of new projects and releases and watch the share price soar. Booya.</p>
<p>So, over the next week or so, you will be treated to:</p>
<p><a href="http://magicmodels.rubyforge.org"><img src="http://drnicwilliams.com/wp-content/uploads/2007/04/magic_models_by_dr_nic_spring.png" alt="Magic Models by Dr Nic - Spring"></a></p>
<p>So call the babysitter, grab yourself a champagne, and find a seat in front of the catwalk: i<strong>ts show time</strong>.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/' rel='bookmark' title='Permanent Link: Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;'>Magic Multi-Connections: A &#8220;facility in Rails to talk to more than one database at a time&#8221;</a> <small>At this point in time there’s no facility in Rails...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2007/04/10/magic-models-the-spring-collection/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Remote Shell with Ruby</title>
		<link>http://drnicwilliams.com/2006/09/22/remote-shell-with-ruby/</link>
		<comments>http://drnicwilliams.com/2006/09/22/remote-shell-with-ruby/#comments</comments>
		<pubDate>Fri, 22 Sep 2006 10:22:50 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2006/09/22/remote-shell-with-ruby/</guid>
		<description><![CDATA[I wrote Composite Primary Keys and Dr Nic&#8217;s Magic Models because we have a 50+ table legacy database and I am terrorised by explicit SQL statements on a daily basis. I have a dirty secret &#8211; I don&#8217;t use either of them at work. We&#8217;ve been unsuccessful installing Ruby on our HP-UX box, then time [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2010/06/01/validate-and-save-your-ruby-in-textmate-with-secret-rubinus-superpowers/' rel='bookmark' title='Permanent Link: Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers'>Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers</a> <small>In some TextMate bundles, if you save a file it...</small></li><li><a href='http://drnicwilliams.com/2008/12/11/future-proofing-your-ruby-code/' rel='bookmark' title='Permanent Link: Future proofing your Ruby code. Ruby 1.9.1 is coming.'>Future proofing your Ruby code. Ruby 1.9.1 is coming.</a> <small> Bugger. I&#8217;m a Ruby monogamist. I use the Ruby...</small></li><li><a href='http://drnicwilliams.com/2008/07/04/unit-testing-iphone-apps-with-ruby-rbiphonetest/' rel='bookmark' title='Permanent Link: Unit Testing iPhone apps with Ruby: rbiphonetest'>Unit Testing iPhone apps with Ruby: rbiphonetest</a> <small> Everything to love about Ruby: the concise, powerful language;...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>I wrote Composite Primary Keys and Dr Nic&#8217;s Magic Models because we have a 50+ table legacy database and I am terrorised by explicit SQL statements on a daily basis. <span class="pullquote">I have a dirty secret &#8211; I don&#8217;t use either of them</span> at work. We&#8217;ve been unsuccessful installing Ruby on our HP-UX box, then time passed, life moved on for the people involved, and so no Ruby. Ergo, SQL hell for Nicholas.</p>
<p>Until today. No, we still don&#8217;t have Ruby installed on our HP box, but I do have it installed everywhere else in my life. So I guess I can still access the database remotely&#8230; that&#8217;s what the TCP/IP internet protocol is for&#8230; and for executing shell commands why not remote shell into the box?</p>
<p>Let&#8217;s skip over why I never thought of either of these until now. Its bad for my ego. The upside of this minor revelation is that the Composite Primary Keys project will have Oracle support soon.</p>
<h3>Remote Shell</h3>
<p>The point of this article though is to discuss the wonders of using remote ssh (aka remote shell) to invoke shell commands via ruby without having ruby installed locally. </p>
<p>I made it work, it rocks and <span class="pullquote">I shall briefly sing the praises for Jamis Buck</span>. Jamis, you rock. <code>net-ssh</code> is a lovely API for performing <a href="http://net-ssh.rubyforge.org/chapter-1.html">remote shell commands</a> all from ruby.  Here&#8217;s a demo of what I can do (excluding the setup code which is discussed later):</p>
<pre>
>> print shell.pwd.stdout
/home/senwilli
>> shell.cd 'demo'
=> #&lt;struct Net::SSH::Service::Shell::SyncShell::CommandOutput stdout="", stderr=nil, status=0&gt;
>> out = shell.ls '-lart'; print out.stdout
total 16
drwxr-xr-x   2 senwilli   sv              96 Sep 22 11:56 .
drwxr-xr-x   5 senwilli   sv            8192 Sep 22 11:56 ..
>> out = shell.send_command 'ls -lart'; print out.stdout
total 16
drwxr-xr-x   2 senwilli   sv              96 Sep 22 11:56 .
drwxr-xr-x   5 senwilli   sv            8192 Sep 22 11:56 ..
</pre>
<p>I&#8217;ve deliberately been inconsistent in the way I called the shell commands and how I handled the responses to show you some interesting things.</p>
<p><code>shell</code> is our shell object (which we&#8217;ll cover in a bit) and it has delightfully overridden <code>method_missing</code> so I can send it any old shell command I like. In the example above, I call the commands: <code>pwd</code>, <code>cd</code> and <code>ls</code> using this method. The first parameter is the arguments you&#8217;d like to pass to the command. An optional second parameter is for any <code>stdin</code> you&#8217;d like to pass to the process during its execution.</p>
<p>The last command is a duplicate of the previous command, showing that you can explicitly pass a complete shell expression string via the <code>send_command</code> method.</p>
<p>The result of all shell commands is a struct containing <code>stdout</code> and <code>stderr</code> strings. So, to return the response of the command to the user or logger, you&#8217;ll need to store the response object (as per the <code>out</code> variable above) and use <code>stdout?</code> to test if <code>stdout</code> has anything in it.</p>
<p>In the example above, all these commands are executedly being executed synchronously, so I&#8217;m getting the stdout/stderr results back before the next command is executed. This is due to the way I created the Shell object. So, let&#8217;s look at that.</p>
<h3>Creating your Shell object</h3>
<p>First you&#8217;ll need to install the following gems (from rubyforge.org) in this order: <code>needle</code>, <code>oepnssl</code>, and <code>net-ssh</code>.</p>
<p>In your ruby script/console:</p>
<pre>
require 'net/ssh'

session = Net::SSH.start( 'host', 'user', 'passwd' )
</pre>
<p>To create our <a href="http://net-ssh.rubyforge.org/chapter-5.html#s4">synchronous shell</a>:</p>
<pre>
shell = session.shell.sync
</pre>
<p>And you&#8217;re off and racing.</p>
<p>Jamis has provided lots of useful syntatic sugar (for example, you can pass Net::SSH.start a block with session as its argument, and reading all the <a href="http://net-ssh.rubyforge.org/chapter-2.html">docco</a> is a clever thing to do next.</p>
<h3>Using sudo?</h3>
<p>The only thing I couldn&#8217;t immediately figure out is how to invoke <code>sudo su - envname</code> and then continue to control my shell as per normal. All commands just blocked.</p>
<p>Luckily there is a <a href="http://net-ssh.rubyforge.org/chapter-1.html#s5">mailing list</a> for me to loiter in&#8230;</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2010/06/01/validate-and-save-your-ruby-in-textmate-with-secret-rubinus-superpowers/' rel='bookmark' title='Permanent Link: Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers'>Validate and Save your Ruby in TextMate &#8211; with secret Rubinus superpowers</a> <small>In some TextMate bundles, if you save a file it...</small></li><li><a href='http://drnicwilliams.com/2008/12/11/future-proofing-your-ruby-code/' rel='bookmark' title='Permanent Link: Future proofing your Ruby code. Ruby 1.9.1 is coming.'>Future proofing your Ruby code. Ruby 1.9.1 is coming.</a> <small> Bugger. I&#8217;m a Ruby monogamist. I use the Ruby...</small></li><li><a href='http://drnicwilliams.com/2008/07/04/unit-testing-iphone-apps-with-ruby-rbiphonetest/' rel='bookmark' title='Permanent Link: Unit Testing iPhone apps with Ruby: rbiphonetest'>Unit Testing iPhone apps with Ruby: rbiphonetest</a> <small> Everything to love about Ruby: the concise, powerful language;...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2006/09/22/remote-shell-with-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Composite migrations for Composite Primary Keys</title>
		<link>http://drnicwilliams.com/2006/08/20/composite-migrations-for-composite-primary-keys/</link>
		<comments>http://drnicwilliams.com/2006/08/20/composite-migrations-for-composite-primary-keys/#comments</comments>
		<pubDate>Sun, 20 Aug 2006 10:53:49 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Link]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2006/08/20/composite-migrations-for-composite-primary-keys/</guid>
		<description><![CDATA[The good people at Err-the-Blog were almost satisfied with the humble offerings of Composite Primary Keys, which provide composite key support for ActiveRecords, including composite foreign keys in assocations. They&#8217;ve gone on an published the main remaining requested feature &#8211; composite migrations for your database table changes. Errtheblog &#8211; Composite Migrations Now, this means that [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/15/acts_as_readonlyable/' rel='bookmark' title='Permanent Link: &#8220;Reads -> slaves, writes -> master&#8221; plugin'>&#8220;Reads -> slaves, writes -> master&#8221; plugin</a> <small>Another solution to multiple connections in Rails has been put...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>The good people at Err-the-Blog were almost satisfied with the humble offerings of <a href="http://compositekeys.rubyforge.org">Composite Primary Keys</a>, which provide composite key support for ActiveRecords, including composite foreign keys in assocations. They&#8217;ve gone on an published the main remaining requested feature &#8211; composite migrations for your database table changes.</p>
<p><a href="http://errtheblog.com/post/16">Errtheblog &#8211; Composite Migrations</a></p>
<p>Now, this means that if you want to use composite foreign keys because you like them you can create as many as you like and DHH can&#8217;t make it hard for you. <img src='http://drnicwilliams.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li><li><a href='http://drnicwilliams.com/2007/04/15/acts_as_readonlyable/' rel='bookmark' title='Permanent Link: &#8220;Reads -> slaves, writes -> master&#8221; plugin'>&#8220;Reads -> slaves, writes -> master&#8221; plugin</a> <small>Another solution to multiple connections in Rails has been put...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2006/08/20/composite-migrations-for-composite-primary-keys/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CPK 0.6.2 &#8211; the &#8220;Santiago&#8221; release</title>
		<link>http://drnicwilliams.com/2006/08/03/cpk-062-the-santiago-release/</link>
		<comments>http://drnicwilliams.com/2006/08/03/cpk-062-the-santiago-release/#comments</comments>
		<pubDate>Thu, 03 Aug 2006 19:53:04 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2006/08/03/cpk-062-the-santiago-release/</guid>
		<description><![CDATA[Thanks to the fine work of Santiago on the forum, some sneaky defects have been found. The new release is now up on rubyforge and available via gem install composite_primary_keys. Thanks to everyone for helping each other solve the &#8220;ActiveRecord hates Composite Primary Keys&#8221; legacy. Bonus thought: Who wants to generate composite primary keys in [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/07/11/where-oh-where-are-my-rails-plugins/' rel='bookmark' title='Permanent Link: Where oh where are my rails plugins?'>Where oh where are my rails plugins?</a> <small>I can&#8217;t be the only person to notice the disappearance...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Thanks to the fine work of Santiago on the <a title="forum" target="_blank" href="http://groups.google.com/group/compositekeys">forum</a>, some sneaky defects have been found. The new release is now up on rubyforge and available via <code>gem install composite_primary_keys</code>.</p>
<p>Thanks to everyone for helping each other solve the &#8220;ActiveRecord hates Composite Primary Keys&#8221; legacy.</p>
<p>Bonus thought: Who wants to generate composite primary keys in their migrations?</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/07/11/where-oh-where-are-my-rails-plugins/' rel='bookmark' title='Permanent Link: Where oh where are my rails plugins?'>Where oh where are my rails plugins?</a> <small>I can&#8217;t be the only person to notice the disappearance...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2006/08/03/cpk-062-the-santiago-release/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Composite Primary Keys supports Associations</title>
		<link>http://drnicwilliams.com/2006/08/02/composite-primary-keys-supports-associations/</link>
		<comments>http://drnicwilliams.com/2006/08/02/composite-primary-keys-supports-associations/#comments</comments>
		<pubDate>Wed, 02 Aug 2006 09:00:31 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2006/08/02/composite-primary-keys-supports-associations/</guid>
		<description><![CDATA[[Post summary: new release with support for ActiveRecord Associations] This project started with an innocent idea as I walked along the street: you could easily write a find method for composite keys. &#8220;What&#8217;s the big deal about composite keys?&#8221; I sat down and wrote the find method. But the crud operations didn&#8217;t work: create, update, [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/08/12/map_by_method-now-works-with-activerecord-associations/' rel='bookmark' title='Permanent Link: map_by_method now works with ActiveRecord associations'>map_by_method now works with ActiveRecord associations</a> <small>I was always annoyed that map_by_method was broken for ActiveRecord...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>[Post summary: new release with support for ActiveRecord Associations]</p>
<p>This project started with an innocent idea as I walked along the street: you could easily write a <code>find</code> method for composite keys. &#8220;What&#8217;s the big deal about composite keys?&#8221;</p>
<p>I sat down and wrote the <code>find</code> method. But the crud operations didn&#8217;t work: create, update, destroy. So I rewrote them for composite key records. I released a gem on various mailing lists to see if the solution worked for other people. People were able to drop it into their legacy system and get it working instantly, and they were thrilled. Cool.</p>
<p>Support for CRUD is only half the job of adding Composite Primary Key support to ActiveRecords/Rails. The other half of ActiveRecord magic is its wonderful support for Associations: <code>has_many</code>, <code>has_one</code>, and <code>belongs_to</code>.</p>
<p>I wrote the association unit tests first and kept extending ActiveRecords until all the tests worked. [A much quicker edit-n-test method than using the console/irb].</p>
<p>And today I uploaded the latest release of Composite Primary Keys with support for Associations. Install it with <code>gem install composite_primary_keys</code> and include <code>require 'composite_primary_keys'</code> in your <code>environment.rb</code> (for Rails apps) or within your Ruby scripts. Visit <a target="_blank" title="http://compositekeys.rubyforge.org" href="http://compositekeys.rubyforge.org">http://compositekeys.rubyforge.org</a> for other information.<br />
Let me know if it works for you!</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2008/06/06/composite-primary-keys-goes-100-for-rails-21/' rel='bookmark' title='Permanent Link: Composite Primary Keys goes 1.0.0 for Rails 2.1'>Composite Primary Keys goes 1.0.0 for Rails 2.1</a> <small>Two years ago Dave Thomas did a keynote at the...</small></li><li><a href='http://drnicwilliams.com/2007/08/12/map_by_method-now-works-with-activerecord-associations/' rel='bookmark' title='Permanent Link: map_by_method now works with ActiveRecord associations'>map_by_method now works with ActiveRecord associations</a> <small>I was always annoyed that map_by_method was broken for ActiveRecord...</small></li><li><a href='http://drnicwilliams.com/2007/05/23/dr-nics-magic-show-at-rejectconf2007/' rel='bookmark' title='Permanent Link: Dr Nic&#8217;s Magic Show at RejectConf2007'>Dr Nic&#8217;s Magic Show at RejectConf2007</a> <small>Update: there is a patch available for edge rails to...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2006/08/02/composite-primary-keys-supports-associations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What is a composite key in Rails?</title>
		<link>http://drnicwilliams.com/2006/08/01/what-is-a-composite-key-in-rails/</link>
		<comments>http://drnicwilliams.com/2006/08/01/what-is-a-composite-key-in-rails/#comments</comments>
		<pubDate>Tue, 01 Aug 2006 06:39:15 +0000</pubDate>
		<dc:creator>Dr Nic</dc:creator>
				<category><![CDATA[Composite Keys]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://drnicwilliams.com/2006/08/01/what-is-a-composite-key-in-rails/</guid>
		<description><![CDATA[First, what is an ActiveRecord primary key currently? There are two versions of a primary key: an integer (12) and a string (&#8217;12&#8242;). That is, Person.find(1) and Person.find(&#8217;1&#8242;) return the same thing. This is the theme of the composite key solution &#8211; any conceivable notion of 2+ numbers should work as well as any other. [...]


Related posts:<ol><li><a href='http://drnicwilliams.com/2010/03/15/using-coffeescript-in-rails-and-even-on-heroku/' rel='bookmark' title='Permanent Link: Using CoffeeScript in Rails and even on Heroku'>Using CoffeeScript in Rails and even on Heroku</a> <small>I&#8217;m pretty excited about CoffeeScript as a clean-syntax replacement for...</small></li><li><a href='http://drnicwilliams.com/2009/11/03/first-look-at-rails-3-0-pre/' rel='bookmark' title='Permanent Link: First look at rails 3.0.pre'>First look at rails 3.0.pre</a> <small> This article is out of date in some aspects....</small></li><li><a href='http://drnicwilliams.com/2009/10/07/rails-themes-can-remember-things/' rel='bookmark' title='Permanent Link: Rails themes can remember things'>Rails themes can remember things</a> <small>I was getting annoyed at having to remember all the...</small></li></ol>]]></description>
			<content:encoded><![CDATA[<p>First, what is an ActiveRecord primary key currently? There are two versions of a primary key: an integer (12) and a string (&#8217;12&#8242;). That is, Person.find(1) and Person.find(&#8217;1&#8242;) return the same thing.</p>
<p>This is the theme of the composite key solution &#8211; any conceivable notion of 2+ numbers should work as well as any other. So, [12,2] should work, as should &#8217;12,2&#8242;, as should ['12','2']. That is, Membership.find(12,2), Membership.find(&#8217;12,2&#8242;), Membership.find([12, 2]), Membership.find(&#8217;12&#8242;,&#8217;2&#8242;), etc, are treated the same.</p>
<p>Normally, if you want to find multiple objects by their primary key, you pass a list, so Person.find(1,2,3) and Person.find(&#8217;1&#8242;,&#8217;2&#8242;,&#8217;3&#8242;) give the same result.</p>
<p>For composite keys, there are many variations that you can use interchangably: Membership.find([12,1],[12,2],[12,3]) is the same as Membership.find(&#8217;12,1&#8242;,&#8217;12,2&#8242;,&#8217;12,3&#8242;) is the same as Membership.find(&#8217;12,1;12,2;12,3&#8242;).</p>
<p>The id of a normal object is the value of its primary key field. So Person.find(1).id == 1, and Person.find(&#8217;1&#8242;).id == 1.</p>
<p>To support interchangable use of the id value within your applications, the composite key result is a CompositeIds object, a subclass of Array where to_s method joins the keys by a comma. Want to pass the id of a composite object to your HTML and back to the server in URLs? You won&#8217;t need to do anything differently than you do now. The to_param method will return a string joined by commas &#8211; e.g. &#8217;12,1&#8242; &#8211; which will be happily accepted by your find method when you receive the value in your controller action methods.</p>
<p>The main code change that you will need to consider is that the primary_key method returns a list of column names, instead of a single column name. (Actually it returns a CompositeKeys instance which subclasses Array, and its to_s method joins the values by a comma).</p>
<p>If you find that Composite Primary Keys isn&#8217;t working in a sexily smooth way, let me know so the interface can be improved.</p>


<p>Related posts:<ol><li><a href='http://drnicwilliams.com/2010/03/15/using-coffeescript-in-rails-and-even-on-heroku/' rel='bookmark' title='Permanent Link: Using CoffeeScript in Rails and even on Heroku'>Using CoffeeScript in Rails and even on Heroku</a> <small>I&#8217;m pretty excited about CoffeeScript as a clean-syntax replacement for...</small></li><li><a href='http://drnicwilliams.com/2009/11/03/first-look-at-rails-3-0-pre/' rel='bookmark' title='Permanent Link: First look at rails 3.0.pre'>First look at rails 3.0.pre</a> <small> This article is out of date in some aspects....</small></li><li><a href='http://drnicwilliams.com/2009/10/07/rails-themes-can-remember-things/' rel='bookmark' title='Permanent Link: Rails themes can remember things'>Rails themes can remember things</a> <small>I was getting annoyed at having to remember all the...</small></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://drnicwilliams.com/2006/08/01/what-is-a-composite-key-in-rails/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
