Dr Nic

Composite Primary Keys supports Associations

[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. “What’s the big deal about composite keys?”

I sat down and wrote the find method. But the crud operations didn’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.

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: has_many, has_one, and belongs_to.

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].

And today I uploaded the latest release of Composite Primary Keys with support for Associations. Install it with gem install composite_primary_keys and include require 'composite_primary_keys' in your environment.rb (for Rails apps) or within your Ruby scripts. Visit http://compositekeys.rubyforge.org for other information.
Let me know if it works for you!

Related posts:

  1. Composite Primary Keys goes 1.0.0 for Rails 2.1 Two years ago Dave Thomas did a keynote at the...
  2. map_by_method now works with ActiveRecord associations I was always annoyed that map_by_method was broken for ActiveRecord...
  3. Dr Nic’s Magic Show at RejectConf2007 Update: there is a patch available for edge rails to...
  4. Magic Multi-Connections: A “facility in Rails to talk to more than one database at a time” At this point in time there’s no facility in Rails...
  5. Magic Models: the Spring collection Over the last few months, we at Dr Nic Magic...

4 Responses to “Composite Primary Keys supports Associations”

  1. Man,

    Thanks so much, it is a great thing. Now I dont need to write ugly sql stuff in order to use regular udpate, update_attributes and so force.

    Is that gem going to be included in rails release ?

  2. Dr Nic says:

    Its unlikely to be in core for the forseeable future as none of the core team use it, so their ability to maintain it is limited by knowledge and enthusiasm :)

    But, its a gem so you can use it for rails and for non-rails scripts. Sexy!

  3. [...] support for more ActiveRecord magic: associations (has_many, belongs_to, etc) Full report: http://drnicwilliams.com/2006/08/02/…-associations/ Cheers Dr Nic — Posted via [...]

  4. tiny says:

    My application use rails 2.3.2 and composite_primary_keys gem.
    It’s good, but i cannot update existing object (obj.save || obj.save!).
    errors: SELECT count(*) AS count_all FROM `listings` WHERE (`listings`.`uuid` = BINARY ‘A41154E5-A3CB-4522-942F-C3247AB6L001′ AND `listings`.portal_uuid,uuid ‘A41154E5-A3CB-4522-942F-C3247AB6C5B1′,’A41154E5-A3CB-4522-942F-C3247AB6L001′).

    is it a bug?