June 2014 Update: This no longer works with the newer versions of the AWS SDK.  This information is retained merely for historical reasons and you should not use the code below.  You have been warned.

In a recent project I needed to save things to DynamoDB and be able to use my own values for the ID. The problem is that by default AWS::Record::HashModel sets the ID field for you automatically and you can't change it. I asked about it on the official AWS forums and the answer was that it can't be done. At least not yet.

So I had to come up with a solution myself. It's not pretty, but Monkey Patching does come in handy sometimes. So, for any others out there who are trying to solve the same problem, he's what I did.
  1. Create a monkey_patch.rb file somewhere in your project where it will be loaded after the main AWS Gem.
  2. Add the following code to the monkey_patch.rb file:

    module AWS
    module Record
    module AbstractBase
    module InstanceMethods

    # Allow the user to override the ID
    def populate_id
    @_id = @_data['id'] || UUIDTools::UUID.random_create.to_s.downcase

  3. Add an "id" field to your AWS::Record::HashModel object.

    class MyModel < AWS::Record::HashModel     
    string_attr :id

  4. Now, whenever you call the create method on MyModel if you provide an ID in the hash then it will get used as the hash_key value.

    MyModel.create{:id => "some_value_that_I_make_up"}

Two things to be aware of, though:
  1. You have to add some other attributes to the model before it will be persisted properly. Just having an "id" won't cause it to save properly.
  2. It's up to you to ensure that you don't create multiple models with the same ID.