Skip to content

Instantly share code, notes, and snippets.

@khustochka
Last active March 22, 2018 18:49
Show Gist options
  • Save khustochka/cab304cfece8752d7d37715013e0cdb8 to your computer and use it in GitHub Desktop.
Save khustochka/cab304cfece8752d7d37715013e0cdb8 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
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"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
#gem "rails", github: "rails/rails"
gem "activerecord", "5.2.0.rc2"
gem "pg"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Need to do this to create a new DB in postgresql.
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "postgres")
ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS test_test")
ActiveRecord::Base.connection.execute("CREATE DATABASE test_test")
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "test_test")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.timestamps
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post, touch: :updated_at
end
class BugTest < Minitest::Test
def test_post_touch
post = Post.create!
post.touch(:updated_at)
end
def test_association_stuff
post = Post.create!
post.comments << Comment.create!
assert_equal 1, post.comments.count
assert_equal 1, Comment.count
assert_equal post.id, Comment.first.post.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment