Last active
August 29, 2015 14:06
-
-
Save girishso/6efaafa138696425defe to your computer and use it in GitHub Desktop.
Template for https://github.com/rails/rails/pull/16698
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
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', github: 'rails/rails' | |
gem 'arel', github: 'rails/arel' | |
gem 'rack', github: 'rack/rack' | |
gem 'i18n', github: 'svenfuchs/i18n' | |
gem 'sqlite3' | |
GEMFILE | |
system 'bundle' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :products, primary_key: "sku" do |t| | |
t.string :name | |
t.belongs_to :stupid_supplier | |
end | |
create_table :versions, primary_key: "vid" do |t| | |
t.belongs_to :product | |
end | |
create_table :stupid_suppliers, primary_key: "sid" do |t| | |
end | |
end | |
class Product < ActiveRecord::Base | |
self.primary_key = "sku" | |
has_many :versions | |
belongs_to :stupid_supplier | |
end | |
class Version < ActiveRecord::Base | |
self.primary_key = "vid" | |
belongs_to :product | |
end | |
class StupidSupplier < ActiveRecord::Base | |
self.primary_key = "sid" | |
has_one :product | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
product = Product.create!(sku: 10) | |
product.stupid_supplier = StupidSupplier.create!(sid: 20) | |
product.versions.create!(vid: 30) | |
assert_equal product.id, Version.first.product_id | |
assert_equal product.versions.first.id, Version.first.id | |
assert_equal product.stupid_supplier_id, StupidSupplier.first.id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment