Skip to content

Instantly share code, notes, and snippets.

@Linuus
Last active October 9, 2017 09:31
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 Linuus/f20e69905e41f2437cbe7589a563136e to your computer and use it in GitHub Desktop.
Save Linuus/f20e69905e41f2437cbe7589a563136e to your computer and use it in GitHub Desktop.
Touch not working on destroy
# 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 "arel", github: "rails/arel"
gem "sqlite3"
end
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 :sites, force: true do |t|
t.datetime :updated_at, null: false
t.datetime :created_at, null: false
end
create_table :site_publications, force: true do |t|
t.integer :site_id
t.integer :guide_id
t.datetime :updated_at, null: false
t.datetime :created_at, null: false
end
create_table :guides, force: true do |t|
t.datetime :updated_at, null: false
t.datetime :created_at, null: false
end
end
class Site < ActiveRecord::Base
has_many :site_publications
has_many :guides, through: :site_publications
end
class SitePublication < ActiveRecord::Base
belongs_to :site, touch: true
belongs_to :guide
end
class Guide < ActiveRecord::Base
has_many :site_publications
has_many :sites, through: :site_publications
end
class BugTest < Minitest::Test
def test_touch_callbacks
site = Site.create!
Guide.create!
Guide.create!
site_updated_at = site.updated_at
site.guide_ids = [1, 2]
assert site_updated_at < site.reload.updated_at, "Site#updated_at should have been updated when publication was created"
site_updated_at = site.reload.updated_at
site.guide_ids = [1]
assert site_updated_at < site.reload.updated_at, "Site#updated_at should have been updated when publication was destroyed"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment