Skip to content

Instantly share code, notes, and snippets.

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 marciojg/146913d661764a4d71cb1ad04182a4b9 to your computer and use it in GitHub Desktop.
Save marciojg/146913d661764a4d71cb1ad04182a4b9 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" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.2.3"
gem "sqlite3", "~> 1.3.6"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# 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 :people, force: true do |t|
t.string :name
end
create_table :matriculations, force: :true do |t|
t.bigint :person_id
end
end
class Matriculation < ActiveRecord::Base
belongs_to :person
accepts_nested_attributes_for :person
validate :fake_validation_with_return_false
def fake_validation_with_return_false
errors.add(:base, 'Fake error') if person.present? && false # Validation always returning false, but the detail is the use of object person.
end
end
class Person < ActiveRecord::Base
has_many :matriculations
validates :name, presence: true
end
class BugTest < Minitest::Test
def test_case
person = Person.create!(name: 'Jhon')
Matriculation.create!(person: person)
person.name = nil
person.save! validate: false # In this moment the object person is invalid. But matriculation isn't.
matriculation = Matriculation.first
assert_equal true, matriculation.valid? # First call
assert_equal false, matriculation.valid? # Second call, return changed??? What?
assert_equal [:"person.name"], matriculation.errors.keys
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment