Skip to content

Instantly share code, notes, and snippets.

@eddiej
Last active August 28, 2015 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddiej/28ad6fc04238348aca94 to your computer and use it in GitHub Desktop.
Save eddiej/28ad6fc04238348aca94 to your computer and use it in GitHub Desktop.
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'deleteme', username: 'root')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table "Manufacturers" do |t|
t.string :manufacturer_name
end
execute "INSERT INTO Manufacturers(manufacturer_name) values('Ford')"
execute "INSERT INTO Manufacturers(manufacturer_name) values('GM')"
execute "INSERT INTO Manufacturers(manufacturer_name) values('Chrysler')"
execute "CREATE VIEW v_manufacturers AS SELECT id, manufacturer_name as name FROM Manufacturers"
create_table :cars do |t|
t.string :name
t.references :manufacturer
t.timestamps
end
add_index :cars, :manufacturer_id
end
class Manufacturer < ActiveRecord::Base
self.table_name = 'v_manufacturers'
self.primary_key = 'id'
end
class Car < ActiveRecord::Base
belongs_to :manufacturer
end
class BugTest < Minitest::Test
def test_association_stuff
assert_equal(nil, Manufacturer.new.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment