Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord. Double validation with autosave: true on both sides #34964

Open
EPecherkin opened this issue Jan 17, 2019 · 4 comments
Open

ActiveRecord. Double validation with autosave: true on both sides #34964

EPecherkin opened this issue Jan 17, 2019 · 4 comments

Comments

@EPecherkin
Copy link
Contributor

EPecherkin commented Jan 17, 2019

Steps to reproduce

#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails"
  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 :stories do |t|
    t.string :type
  end

  create_table :bad_guys do |t|
    t.references :story
  end
end

::VALIDATED = 0

class Story < ActiveRecord::Base
  has_one :bad_guy, foreign_key: :story_id, inverse_of: :story, autosave: true

  validate { ::VALIDATED += 1 }
end

class BadGuy < ActiveRecord::Base
  belongs_to :story, inverse_of: :bad_guy, autosave: true
end

class TestDoubleValidation < Minitest::Test
  def test_validated_once
    story = Story.new()
    bad_guy = BadGuy.new(story: story)
    story.save!
    assert_equal 1, VALIDATED
  end
end

Expected behavior

Story should be validated only once

Actual behavior

Story validated twice

System configuration

Rails version: master, 5.2.0

Ruby version: 2.5.1

@shime
Copy link
Contributor

shime commented Jan 20, 2019

Setting the validate to false for association in BadGuy would make the test pass.

class BadGuy < ActiveRecord::Base
  belongs_to :story, class_name: 'DetectiveNovel', inverse_of: :bad_guy, autosave: true, validate: false
end

@EPecherkin does that help?

@EPecherkin
Copy link
Contributor Author

@shime it doesn't really. I need to have autosave: true on both sides

@EPecherkin EPecherkin changed the title ActiveRecord. Double validation with STI and autosave ActiveRecord. Double validation with autosave: true on both sides Jan 21, 2019
@robertasg
Copy link

I've opened a PR a while ago to take care of this #31381

I assume this should solve your issue. I'll rebase it a bit. We can try to work off that.

@mvaragnat
Copy link

I'd love to see the PR merge. Right now we have the issue and use an ugly fix (override changed_for_autosave? in the parent in the specific case causing us problem)

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

No branches or pull requests

5 participants