Skip to content

Instantly share code, notes, and snippets.

@hayesr
Created August 23, 2015 04:28
Show Gist options
  • Save hayesr/3756183d34a6305d5143 to your computer and use it in GitHub Desktop.
Save hayesr/3756183d34a6305d5143 to your computer and use it in GitHub Desktop.
Timestamp columns are not auto populated for hm:t join tables
ENV['FIXTURES_DIR'] = "./"
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 'sqlite3'
end
require 'active_record'
require 'active_record/fixtures'
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 :students, force: true do |t|
end
create_table :classrooms, force: true do |t|
end
create_table :enrollments, force: true do |t|
t.integer :classroom_id
t.integer :student_id
t.timestamps null: false
end
end
class Student < ActiveRecord::Base
has_many :enrollments
has_many :classrooms, through: :enrollments
end
class Classroom < ActiveRecord::Base
has_many :enrollments
has_many :students, through: :enrollments
end
class Enrollment < ActiveRecord::Base
belongs_to :student
belongs_to :classroom
end
class BugTest < Minitest::Test
def test_hmt_fixture_loading
# One expects these 2 models to be enough, but including enrollments does not work either
ActiveRecord::FixtureSet.create_fixtures('./', ['classrooms', 'students'])
# => ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed ...
end
end
teachers_class: {}
# Attempting to load times explicitly has no effect
one:
classroom: teachers_class
student: jonny
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
jonny:
classrooms: teachers_class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment