Skip to content

Instantly share code, notes, and snippets.

@claudiob
Created August 10, 2016 22:16
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 claudiob/545fa683615bbc7cb6d8ddc99c4306c1 to your computer and use it in GitHub Desktop.
Save claudiob/545fa683615bbc7cb6d8ddc99c4306c1 to your computer and use it in GitHub Desktop.
Failing test for has_many / dependent with foreign key
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 "activerecord", "5.0.0"
gem "pg"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection adapter: 'postgresql'
ActiveRecord::Base.connection.drop_database 'test_has_many_foreign_key'
ActiveRecord::Base.connection.create_database 'test_has_many_foreign_key'
ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'test_has_many_foreign_key'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.references :post, foreign_key: true
t.string :text
end
end
class Post < ActiveRecord::Base
has_many :comments, -> { where('text is not null') }, dependent: :destroy
end
class Comment < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_has_many_foreign_key
post = Post.create!
empty = Comment.create! post_id: post.id
valid = Comment.create! post_id: post.id, text: 'some text'
post.destroy
assert_equal 0, Post.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment