Skip to content

Instantly share code, notes, and snippets.

@ronalchn
Last active January 1, 2016 02:19
Show Gist options
  • Save ronalchn/8078852 to your computer and use it in GitHub Desktop.
Save ronalchn/8078852 to your computer and use it in GitHub Desktop.
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.2'
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 :posts do |t|
end
create_table :comments do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy, inverse_of: :post # setting inverse does not change behaviour of tests
end
class Comment < ActiveRecord::Base
belongs_to :post, touch: true, inverse_of: :comments
end
class BugTest < Minitest::Unit::TestCase
def test_association_working
comment = Comment.create(post: Post.new)
post = comment.post
comment = Comment.find(comment.id)
post.destroy
comment.destroy
end
def test_association_failing
comment = Comment.create(post: Post.new)
comment.post.destroy
comment.destroy #=> ActiveRecord::ActiveRecordError: can not touch on a new record object
end
def test_post_destroyed
comment = Comment.create(post: Post.new)
comment.post.destroy
refute comment.post.persisted?
end
def test_comment_destroyed
comment = Comment.create(post: Post.new)
comment.post.destroy
refute comment.persisted?, 'destroying post should destroy comment'
end
end
@ronalchn
Copy link
Author

  1) Failure:
BugTest#test_comment_destroyed [touch_destroy_bug.rb:50]:
destroying post should destroy comment

  2) Error:
BugTest#test_association_failing:
ActiveRecord::ActiveRecordError: can not touch on a new record object
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/persistence.rb:431:in `touch'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/callbacks.rb:296:in `block in touch'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__242324728__touch__callbacks'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/callbacks.rb:296:in `touch'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/associations/builder/belongs_to.rb:86:in `belongs_to_touch_after_save_or_destroy_for_post'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:375:in `_run__84165116__destroy__callbacks'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/callbacks.rb:292:in `destroy'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:265:in `block in destroy'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
    /home/ronald/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:265:in `destroy'
    touch_destroy_bug.rb:40:in `test_association_failing'

4 tests, 2 assertions, 1 failures, 1 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment