Permalink
Browse files

Fix a bug where default_scope was overriding attributes given on mode…

…l initialization [#3218 status:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 79c47ab commit bf9a0ae12b701cba7a8aac2955ce243866ac7bf6 @hsume2 hsume2 committed with josevalim Feb 26, 2010
@@ -1669,12 +1669,12 @@ def initialize(attributes = nil)
@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 @@ def test_default_scope_using_relation
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

0 comments on commit bf9a0ae

Please sign in to comment.