Last active
January 1, 2016 02:19
-
-
Save ronalchn/8078852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Author
ronalchn
commented
Dec 22, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment