# ActiveRecord: Legacy primary keys

**URL:** <https://www.ruby-forum.com/t/activerecord-legacy-primary-keys/74507>\
**Category:** Rails\
**Created:** [February 28, 2006, 4:00am UTC](https://www.ruby-forum.com/t/activerecord-legacy-primary-keys/74507 "2006-02-28T04:00:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [February 28, 2006, 4:00am UTC](https://www.ruby-forum.com/t/activerecord-legacy-primary-keys/74507/1 "2006-02-28T04:00:42Z")

</div>

Hi,  
I’m building a frontend for a legacy schema, with tables like this:  
CREATE TABLE `types` (  
`a_type_cd` varchar(6) NOT NULL default ‘’,  
`a_type` varchar(30) NOT NULL default ‘’,  
PRIMARY KEY (`a_type_cd`)  
);

The primary key is specified by the user (not auto-generated), and needs  
to be updateable. I create the following a base class to support this,  
which I would like some feedback on. It works with scaffold generated  
code. Specifically:

- I feel like there should be a better way to do the “update” method.  
(It is overridden so that you can update the primary key)
- Is overriding self.inherited a good idea?
- This implementation works for me, are there any other features anyone  
can think of that they may require in this instance?

Usage:  
class AType \< UserPKActiveRecord  
set\_primary\_key ‘a\_type\_cd’  
end

Base Class:  
class UserPKActiveRecord \< ActiveRecord::Base  
def self.inherited(sub)  
set\_table\_name undecorated\_table\_name(sub)

```
self.class_eval <<-EOS
  def initialize params = nil
   super
   self.id = params[self.class.primary_key] if params && 

```

params.has\_key?(self.class.primary\_key)  
end  
EOS  
end

def self.set\_primary\_key pk  
super

```
alias_method pk, :id
alias_method pk + '=', :id=

self.class_eval <<-EOS
  def #{pk}_before_type_cast
    return read_attribute(self.class.primary_key) if 

```

read\_attribute(self.class.primary\_key)  
return self.id  
end  
EOS  
end

def self.content\_columns  
@content\_columns = columns.reject { |c| c.name =~ /(\_id|\_count)$/ ||  
c.name == inheritance\_column }  
end

def update  
if !@new\_id  
sql = "UPDATE #{self.class.table\_name} " +  
"SET #{quoted\_comma\_pair\_list(connection,  
attributes\_with\_quotes(false))} " +  
“WHERE #{self.class.primary\_key} = #{quote(id)}”  
else  
sql = "UPDATE #{self.class.table\_name} " +  
"SET #{quoted\_comma\_pair\_list(connection,  
attributes\_with\_quotes(false))} " +  
", #{self.class.primary\_key} = #{quote(@new\_id)} " +  
“WHERE #{self.class.primary\_key} = #{quote(id)}”  
end  
puts sql  
connection.update(sql, “#{self.class.name} Update”)  
end

def update\_attributes(attributes)  
if attributes.has\_key?(self.class.primary\_key)  
@new\_id = attributes[self.class.primary\_key]  
end  
result = super  
self.id = @new\_id  
@new\_id = nil  
return result  
end  
end
