h1. Composite Primary Keys h1. → Ruby on Rails h1. → ActiveRecords h2. What Ruby on Rails does not support composite primary keys. This free software is an extension to the database layer of Rails - "ActiveRecords":http://wiki.rubyonrails.com/rails/pages/ActiveRecord - to support composite primary keys as transparently as possible. Any Ruby script using ActiveRecords can use Composite Primary Keys with this library. h2. Installing
sudo gem install composite_primary_keys
Rails: Add the following to the bottom of your environment.rb file
require 'composite_primary_keys'
Ruby scripts: Add the following to the top of your script
require 'rubygems'
require 'composite_primary_keys'
h2. The basics A model with composite primary keys would look like...
class Membership < ActiveRecord::Base
  # set_primary_keys *keys - turns on composite key functionality
  set_primary_keys :user_id, :group_id
  belongs_to :user
  belongs_to :group
  has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
end
A model associated with a composite key model would be defined like...
class MembershipStatus < ActiveRecord::Base
  belongs_to :membership, :foreign_key => [:user_id, :group_id]
end
That is, associations can include composite keys too. Nice. h2. Demonstration of usage Once you've created your models to specify composite primary keys (such as the Membership class) and associations (such as MembershipStatus#membership), you can uses them like any normal model with associations. But first, lets check out our primary keys.
MembershipStatus.primary_key # => "id"    # normal single key
Membership.primary_key  # => [:user_id, :group_id] # composite keys
Membership.primary_key.to_s # => "user_id,group_id"
Now we want to be able to find instances using the same syntax we always use for ActiveRecords...
MembershipStatus.find(1)    # single id returns single instance
=> "1", "status"=>"Active"}>
Membership.find(1,1)  # composite ids returns single instance
=> "1", "group_id"=>"1"}>
Using "Ruby on Rails":http://www.rubyonrails.org? You'll want to your url_for helpers to convert composite keys into strings and back again...
Membership.find(:first).to_param # => "1,1"
And then use the string id within your controller to find the object again
params[:id] # => '1,1'
Membership.find(params[:id])
=> "1", "group_id"=>"1"}>
That is, an ActiveRecord supporting composite keys behaves transparently throughout your application. Just like a normal ActiveRecord. h2. Other tricks h3. Pass a list of composite ids to the #find method
Membership.find [1,1], [2,1]
=> [
  "1", "group_id"=>"1"}>, 
  "2", "group_id"=>"1"}>
]
Perform #count operations
MembershipStatus.find(:first).memberships.count # => 1
h3. Routes with Rails From Pete Sumskas:
I ran into one problem that I didn't see mentioned on "this list":http://groups.google.com/group/compositekeys - and I didn't see any information about what I should do to address it in the documentation (might have missed it). The problem was that the urls being generated for a 'show' action (for example) had a syntax like:
/controller/show/123000,Bu70
for a two-field composite PK. The default routing would not match that, so after working out how to do the routing I added:
map.connect ':controller/:action/:id', :id => /\w+(,\w+)*/
to my route.rb file.
h2. Which databases? A suite of unit tests have been run on the following databases supported by ActiveRecord: |_.Database|_.Test Success|_.User feedback| |mysql |YES|YES ("Yes!":mailto:compositekeys@googlegroups.com?subject=Mysql+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Mysql+is+failing)| |sqlite3 |YES|YES ("Yes!":mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Sqlite3+is+failing)| |postgresql|YES|YES ("Yes!":mailto:compositekeys@googlegroups.com?subject=Postgresql+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Postgresql+is+failing)| |oracle |YES|YES ("Yes!":mailto:compositekeys@googlegroups.com?subject=Oracle+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Oracle+is+failing)| |sqlserver |??? ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+SQLServer)|??? ("Yes!":mailto:compositekeys@googlegroups.com?subject=SQLServer+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=SQLServer+is+failing)| |db2 |??? ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+DB2)|??? ("Yes!":mailto:compositekeys@googlegroups.com?subject=DB2+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=DB2+is+failing)| |firebird |??? ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Firebird)|??? ("Yes!":mailto:compositekeys@googlegroups.com?subject=Firebird+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Firebird+is+failing)| |sybase |??? ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Sybase)|??? ("Yes!":mailto:compositekeys@googlegroups.com?subject=Sybase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Sybase+is+failing)| |openbase |??? ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Openbase)|??? ("Yes!":mailto:compositekeys@googlegroups.com?subject=Openbase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Openbase+is+failing)| |frontbase |??? ("I can help":mailto:compositekeys@googlegroups.com?subject=Help+with+Frontbase)|??? ("Yes!":mailto:compositekeys@googlegroups.com?subject=Frontbase+is+working or "No...":mailto:compositekeys@googlegroups.com?subject=Frontbase+is+failing)| h2. Dr Nic's Blog "http://www.drnicwilliams.com":http://www.drnicwilliams.com - for future announcements and other stories and things. h2. Forum "http://groups.google.com/group/compositekeys":http://groups.google.com/group/compositekeys h2. How to submit patches Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above. The source for this project is available via git. You can "browse and/or fork the source":http://github.com/drnic/composite_primary_keys/tree/master, or to clone the project locally:
git clone git://github.com/drnic/composite_primary_keys.git
h2. Licence This code is free to use under the terms of the MIT licence. h2. Contact Comments are welcome. Send an email to "Dr Nic Williams":mailto:drnicwilliams@gmail.com.