mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #22300 from yui-knk/fix_21893
Except keys of `build_record`'s argument from `create_scope` in initi…
This commit is contained in:
commit
bde974f418
5 changed files with 23 additions and 5 deletions
|
@ -163,9 +163,12 @@ module ActiveRecord
|
||||||
@reflection = @owner.class._reflect_on_association(reflection_name)
|
@reflection = @owner.class._reflect_on_association(reflection_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize_attributes(record) #:nodoc:
|
def initialize_attributes(record, except_from_scope_attributes = nil) #:nodoc:
|
||||||
|
except_from_scope_attributes ||= {}
|
||||||
skip_assign = [reflection.foreign_key, reflection.type].compact
|
skip_assign = [reflection.foreign_key, reflection.type].compact
|
||||||
attributes = create_scope.except(*(record.changed - skip_assign))
|
assigned_keys = record.changed
|
||||||
|
assigned_keys += except_from_scope_attributes.keys.map(&:to_s)
|
||||||
|
attributes = create_scope.except(*(assigned_keys - skip_assign))
|
||||||
record.assign_attributes(attributes)
|
record.assign_attributes(attributes)
|
||||||
set_inverse_instance(record)
|
set_inverse_instance(record)
|
||||||
end
|
end
|
||||||
|
@ -248,7 +251,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def build_record(attributes)
|
def build_record(attributes)
|
||||||
reflection.build_association(attributes) do |record|
|
reflection.build_association(attributes) do |record|
|
||||||
initialize_attributes(record)
|
initialize_attributes(record, attributes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -203,9 +203,22 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
bulb = car.bulbs.create
|
bulb = car.bulbs.create
|
||||||
assert_equal 'defaulty', bulb.name
|
assert_equal 'defaulty', bulb.name
|
||||||
|
end
|
||||||
|
|
||||||
bulb = car.bulbs.create(:name => 'exotic')
|
def test_build_and_create_from_association_should_respect_passed_attributes_over_default_scope
|
||||||
|
car = Car.create(name: 'honda')
|
||||||
|
|
||||||
|
bulb = car.bulbs.build(name: 'exotic')
|
||||||
assert_equal 'exotic', bulb.name
|
assert_equal 'exotic', bulb.name
|
||||||
|
|
||||||
|
bulb = car.bulbs.create(name: 'exotic')
|
||||||
|
assert_equal 'exotic', bulb.name
|
||||||
|
|
||||||
|
bulb = car.awesome_bulbs.build(frickinawesome: false)
|
||||||
|
assert_equal false, bulb.frickinawesome
|
||||||
|
|
||||||
|
bulb = car.awesome_bulbs.create(frickinawesome: false)
|
||||||
|
assert_equal false, bulb.frickinawesome
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_build_from_association_should_respect_scope
|
def test_build_from_association_should_respect_scope
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
class Bulb < ActiveRecord::Base
|
class Bulb < ActiveRecord::Base
|
||||||
default_scope { where(:name => 'defaulty') }
|
default_scope { where(:name => 'defaulty') }
|
||||||
belongs_to :car, :touch => true
|
belongs_to :car, :touch => true
|
||||||
|
scope :awesome, -> { where(frickinawesome: true) }
|
||||||
|
|
||||||
attr_reader :scope_after_initialize, :attributes_after_initialize
|
attr_reader :scope_after_initialize, :attributes_after_initialize
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ class Car < ActiveRecord::Base
|
||||||
has_many :funky_bulbs, class_name: 'FunkyBulb', dependent: :destroy
|
has_many :funky_bulbs, class_name: 'FunkyBulb', dependent: :destroy
|
||||||
has_many :failed_bulbs, class_name: 'FailedBulb', dependent: :destroy
|
has_many :failed_bulbs, class_name: 'FailedBulb', dependent: :destroy
|
||||||
has_many :foo_bulbs, -> { where(:name => 'foo') }, :class_name => "Bulb"
|
has_many :foo_bulbs, -> { where(:name => 'foo') }, :class_name => "Bulb"
|
||||||
|
has_many :awesome_bulbs, -> { awesome }, class_name: "Bulb"
|
||||||
|
|
||||||
has_one :bulb
|
has_one :bulb
|
||||||
|
|
||||||
|
|
|
@ -114,7 +114,7 @@ ActiveRecord::Schema.define do
|
||||||
create_table :bulbs, force: true do |t|
|
create_table :bulbs, force: true do |t|
|
||||||
t.integer :car_id
|
t.integer :car_id
|
||||||
t.string :name
|
t.string :name
|
||||||
t.boolean :frickinawesome
|
t.boolean :frickinawesome, default: false
|
||||||
t.string :color
|
t.string :color
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue