Created
September 1, 2011 23:02
-
-
Save rsutphin/1187521 to your computer and use it in GitHub Desktop.
ActiveRecord 3.1 cannot load models with non-default PKs unless there's already a database connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'fileutils' | |
DATABASE_NAME = 'people.sqlite3' | |
FileUtils.rm_rf DATABASE_NAME, :verbose => true | |
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DATABASE_NAME | |
ActiveRecord::Schema.define do | |
create_table :people, :id => false do |t| | |
t.primary_key :pid | |
t.string :name, :null => false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
Bundler.setup | |
require 'active_record' | |
def load_models | |
puts "Loading models" | |
require File.expand_path('../models', __FILE__) | |
end | |
def connect_to_database | |
puts "Creating and connecting to database" | |
require File.expand_path('../database', __FILE__) | |
end | |
def models_first? | |
ARGV.first =~ /first/ | |
end | |
def msg | |
"on ActiveRecord #{ActiveRecord::VERSION::STRING} with models loaded #{models_first? ? 'first' : 'second'}" | |
end | |
begin | |
puts "== Running #{msg}" | |
if models_first? | |
load_models | |
connect_to_database | |
else | |
connect_to_database | |
load_models | |
end | |
Person.create!(:name => 'Jo') | |
puts "-- Successful #{msg}!" | |
rescue => e | |
puts "-- Failed #{msg}:" | |
raise e | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'activerecord', ENV['AR_VERSION'] || '3.1.0' | |
gem 'sqlite3', '~> 1.3.4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
class Person < ActiveRecord::Base | |
set_primary_key :pid | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sh ./run.sh | |
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0] | |
Fetching source index for http://rubygems.org/ | |
Using activesupport (3.0.10) | |
Using builder (2.1.2) | |
Using i18n (0.5.0) | |
Using activemodel (3.0.10) | |
Using arel (2.0.10) | |
Using tzinfo (0.3.29) | |
Using activerecord (3.0.10) | |
Using sqlite3 (1.3.4) | |
Using bundler (1.0.15) | |
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed. | |
== Running on ActiveRecord 3.0.10 with models loaded first | |
Loading models | |
Creating and connecting to database | |
rm -rf people.sqlite3 | |
-- create_table(:people, {:id=>false}) | |
-> 0.0667s | |
-- Successful on ActiveRecord 3.0.10 with models loaded first! | |
== Running on ActiveRecord 3.0.10 with models loaded second | |
Creating and connecting to database | |
rm -rf people.sqlite3 | |
-- create_table(:people, {:id=>false}) | |
-> 0.0700s | |
Loading models | |
-- Successful on ActiveRecord 3.0.10 with models loaded second! | |
Fetching source index for http://rubygems.org/ | |
Using multi_json (1.0.3) | |
Using activesupport (3.1.0) | |
Using bcrypt-ruby (3.0.0) | |
Using builder (3.0.0) | |
Using i18n (0.6.0) | |
Using activemodel (3.1.0) | |
Using arel (2.2.1) | |
Using tzinfo (0.3.29) | |
Using activerecord (3.1.0) | |
Using sqlite3 (1.3.4) | |
Using bundler (1.0.15) | |
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed. | |
== Running on ActiveRecord 3.1.0 with models loaded first | |
Loading models | |
-- Failed on ActiveRecord 3.1.0 with models loaded first: | |
/Users/rsutphin/.rvm/gems/ruby-1.9.2-p290@tmp/gems/activerecord-3.1.0/lib/active_record/attribute_methods/primary_key.rb:69:in `set_primary_key': undefined method `primary_keys' for nil:NilClass (NoMethodError) | |
from /private/tmp/ar-cp-pk/models.rb:4:in `<class:Person>' | |
from /private/tmp/ar-cp-pk/models.rb:3:in `<top (required)>' | |
from example.rb:8:in `require' | |
from example.rb:8:in `load_models' | |
from example.rb:28:in `<main>' | |
== Running on ActiveRecord 3.1.0 with models loaded second | |
Creating and connecting to database | |
rm -rf people.sqlite3 | |
-- create_table(:people, {:id=>false}) | |
-> 0.0068s | |
Loading models | |
-- Successful on ActiveRecord 3.1.0 with models loaded second! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
function run_example { | |
export AR_VERSION=$1 | |
bundle update | |
bundle exec ruby example.rb first | |
echo | |
bundle exec ruby example.rb second | |
echo | |
} | |
echo `ruby -v` | |
run_example '3.0.10' | |
run_example '3.1.0' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a test case for rails #2807. Clone it, then run
run.sh
to show that the load order doesn't matter on AR 3.0 but does on AR 3.1.