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

Nested attributes are not updated whenhas_many is present in both parent and child STI models #33305

Closed
sergey-alekseev opened this issue Jul 6, 2018 · 0 comments

Comments

@sergey-alekseev
Copy link
Contributor

Steps to reproduce

# 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 :companies do |t|
    t.string :type
  end

  create_table :employees do |t|
    t.references :company, foreign_key: true
    t.string :name
  end
end

class Company < ActiveRecord::Base
  has_many :employees
end

class PublicCompany < Company
  has_many :employees, foreign_key: :company_id

  accepts_nested_attributes_for :employees
end

class Employee < ActiveRecord::Base
  belongs_to :company
end

class BugTest < Minitest::Test
  def test_nested_attributes_with_sti_and_has_many
    public_company = PublicCompany.create!
    public_company_employee = Employee.create!(name: 'Initial', company: public_company)
    public_company.update!({
      'employees_attributes' => {
        '0' => {
          'name' => 'Updated',
          'id' => public_company_employee.id
        }
      }
    })
    public_company_employee.reload

    assert_equal 'Updated', public_company_employee.name
  end
end

Expected behavior

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Actual behavior

Failure:
BugTest#test_nested_attributes_with_sti_and_has_many:
Expected: "Updated"
  Actual: "Initial"

1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

System configuration

Rails version: 6.0.0.alpha
Ruby version: 2.5.1

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

3 participants