mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix a bug where default_scope was overriding attributes given on model initialization [#3218 status:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
79c47abe6c
commit
bf9a0ae12b
3 changed files with 16 additions and 1 deletions
|
@ -1669,12 +1669,12 @@ module ActiveRecord #:nodoc:
|
|||
@attributes_cache = {}
|
||||
@new_record = true
|
||||
ensure_proper_type
|
||||
self.attributes = attributes unless attributes.nil?
|
||||
|
||||
if scope = self.class.send(:current_scoped_methods)
|
||||
create_with = scope.scope_for_create
|
||||
create_with.each { |att,value| self.send("#{att}=", value) } if create_with
|
||||
end
|
||||
self.attributes = attributes unless attributes.nil?
|
||||
|
||||
result = yield self if block_given?
|
||||
_run_initialize_callbacks
|
||||
|
|
|
@ -663,6 +663,16 @@ class DefaultScopingTest < ActiveRecord::TestCase
|
|||
assert_equal 2, posts.count
|
||||
assert_equal posts(:thinking), posts.first
|
||||
end
|
||||
|
||||
def test_create_attribute_overwrites_default_scoping
|
||||
assert_equal 'David', PoorDeveloperCalledJamis.create!(:name => 'David').name
|
||||
assert_equal 200000, PoorDeveloperCalledJamis.create!(:name => 'David', :salary => 200000).salary
|
||||
end
|
||||
|
||||
def test_create_attribute_overwrites_default_values
|
||||
assert_equal nil, PoorDeveloperCalledJamis.create!(:salary => nil).salary
|
||||
assert_equal 50000, PoorDeveloperCalledJamis.create!(:name => 'David').salary
|
||||
end
|
||||
end
|
||||
|
||||
=begin
|
||||
|
|
|
@ -99,3 +99,8 @@ class DeveloperCalledJamis < ActiveRecord::Base
|
|||
self.table_name = 'developers'
|
||||
default_scope :conditions => { :name => 'Jamis' }
|
||||
end
|
||||
|
||||
class PoorDeveloperCalledJamis < ActiveRecord::Base
|
||||
self.table_name = 'developers'
|
||||
default_scope :conditions => { :name => 'Jamis', :salary => 50000 }
|
||||
end
|
Loading…
Reference in a new issue