Explicit Primary Key with ActiveRecord

If you need to use an explicit primary key and not an AUTO_INCREMENT with ActiveRecord I wrote this snippet that has proved very handy:

module ExplicitPrimaryKey
  attr_accessor :pk
  def before_create; self.id = pk; end
end

Then when you call the create method supply :pk => value along with the other values.

ActiveRecord is an ORM solution that grew out of Ruby on Rails. It is entirely separate and can be used outside of Rails.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.