1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix error when saving an association with a relation named record

ArgumentError: wrong number of arguments (given 3, expected 0)
    /Users/dorianmariefr/.rvm/rubies/ruby-3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-7.0.1/lib/active_record/associations/builder/belongs_to.rb:132:in `record_changed?'
    /Users/dorianmariefr/.rvm/rubies/ruby-3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-7.0.1/lib/active_record/autosave_association.rb:450:in `save_has_one_association'
This commit is contained in:
Dorian Marié 2022-01-17 21:46:54 +01:00
parent 9fb6cdf8fa
commit 61ea3c286d
5 changed files with 42 additions and 2 deletions

View file

@ -446,7 +446,7 @@ module ActiveRecord
elsif autosave != false
key = reflection.options[:primary_key] ? public_send(reflection.options[:primary_key]) : id
if (autosave && record.changed_for_autosave?) || record_changed?(reflection, record, key)
if (autosave && record.changed_for_autosave?) || _record_changed?(reflection, record, key)
unless reflection.through_reflection
record[reflection.foreign_key] = key
association.set_inverse_instance(record)
@ -461,7 +461,7 @@ module ActiveRecord
end
# If the record is new or it has changed, returns true.
def record_changed?(reflection, record, key)
def _record_changed?(reflection, record, key)
record.new_record? ||
association_foreign_key_changed?(reflection, record, key) ||
record.will_save_change_to_attribute?(reflection.foreign_key)

View file

@ -34,6 +34,8 @@ require "models/organization"
require "models/guitar"
require "models/tuning_peg"
require "models/reply"
require "models/attachment"
require "models/translation"
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
def test_autosave_works_even_when_other_callbacks_update_the_parent_model
@ -2008,3 +2010,14 @@ class TestAutosaveAssociationOnAHasManyAssociationDefinedInSubclassWithAcceptsNe
assert_equal "Updated", valid_project.name
end
end
class TestAutosaveAssociationOnABelongsToAssociationDefinedAsRecord < ActiveRecord::TestCase
def test_should_not_raise_error
translation = Translation.create(locale: "fr", key: "bread", value: "Baguette 🥖")
author = Author.create(name: "Dorian Marié")
translation.build_attachment(record: author)
assert_nothing_raised do
translation.save!
end
end
end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class Attachment < ActiveRecord::Base
belongs_to :record, polymorphic: true
has_one :translation
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class Translation < ActiveRecord::Base
belongs_to :attachment, optional: true
validates :locale, presence: true
validates :key, presence: true
validates :value, presence: true
end

View file

@ -58,6 +58,10 @@ ActiveRecord::Schema.define do
t.references :tag
end
create_table :attachments, force: true do |t|
t.references :record, polymorphic: true, null: false
end
create_table :audit_logs, force: true do |t|
t.column :message, :string, null: false
t.column :developer_id, :integer, null: false
@ -1107,6 +1111,13 @@ ActiveRecord::Schema.define do
t.datetime :updated_at
end
create_table :translations, force: true do |t|
t.string :locale, null: false
t.string :key, null: false
t.string :value, null: false
t.references :attachment
end
create_table :tuning_pegs, force: true do |t|
t.integer :guitar_id
t.float :pitch