Skip to content

Instantly share code, notes, and snippets.

@gdeglin
Created September 20, 2015 23:24
Show Gist options
  • Save gdeglin/7be2e39ea29cb59c1409 to your computer and use it in GitHub Desktop.
Save gdeglin/7be2e39ea29cb59c1409 to your computer and use it in GitHub Desktop.
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'sprockets', github: 'rails/sprockets'
gem 'sprockets-rails', github: 'rails/sprockets-rails'
gem 'sass-rails', github: 'rails/sass-rails'
gem 'pg'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'test')
ActiveRecord
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
execute %q(
drop schema public cascade;
create schema public;
)
create_table :users, force: true do |t|
end
create_table :profiles, force: true do |t|
t.integer :user_id
end
execute %q(
do $$
begin
execute format('CREATE TABLE profiles_%s ( like profiles including all )', '1' );
execute format('ALTER TABLE profiles_%s inherit profiles' , '1');
execute format('ALTER TABLE profiles_%s add constraint partitioning_check check ( id >= ''%s''AND id < ''%s'')', '1', '1', '1000' );
execute format('CREATE TABLE profiles_%s ( like profiles including all )', '2' );
execute format('ALTER TABLE profiles_%s inherit profiles' , '2');
execute format('ALTER TABLE profiles_%s add constraint partitioning_check check ( id >= ''%s'')', '2', '1000' );
end
$$;
)
execute %q(
CREATE OR REPLACE FUNCTION profiles_insert_triger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.id >= '1' AND
NEW.id < '1000' ) THEN
INSERT INTO profiles_1 VALUES (NEW.*);
ELSIF ( NEW.id >= '1000') THEN
INSERT INTO profiles_2 VALUES (NEW.*);
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
)
execute %q(
CREATE TRIGGER insert_profiles_trigger
BEFORE INSERT ON profiles
FOR EACH ROW EXECUTE PROCEDURE profiles_insert_triger();
)
end
class User < ActiveRecord::Base
has_one :profile
after_save do |user|
puts "------Before create profile"
user.create_profile
puts "------After create profile"
end
end
class Profile < ActiveRecord::Base
belongs_to :user
end
class BugTest < Minitest::Test
def test_association_stuff
user = User.create
assert Profile.first.user_id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment