public
Created

  • Download Gist
active_record_base_fix.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Patch for rails 3.1 below
# For master s/connection_pool/connection.schema_cache/
 
module ActiveRecord
class Base
class << self
...
def columns
if defined?(@primary_key)
if connection_pool.columns.has_key?(table_name) && connection_pool.primary_keys[table_name].nil?
connection_pool.columns.delete table_name
connection_pool.columns_hash.delete table_name
end
connection_pool.primary_keys[table_name] ||= primary_key
end
 
connection_pool.columns[table_name]
end
...
end
end
end
failing_primary_key_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
## must run activerecord's `rake postgresql:build_databases`
require 'active_record'
 
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :database => 'activerecord_unittest')
 
ActiveRecord::Schema.define do
execute <<_SQL
CREATE SEQUENCE foo_seq;
CREATE TABLE posts(
foo int NOT NULL DEFAULT nextval( 'foo_seq'::regclass )
);
_SQL
end
 
class Post < ActiveRecord::Base
self.primary_key = :foo
end
 
begin
p Post.create
p Post.primary_key
ensure
ActiveRecord::Base.connection.execute <<_SQL
DROP TABLE posts;
DROP SEQUENCE foo_seq;
_SQL
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.