diff options
author | Henry Hsu <henry@qlane.com> | 2010-02-26 11:06:55 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-02-26 11:09:43 +0100 |
commit | bf9a0ae12b701cba7a8aac2955ce243866ac7bf6 (patch) | |
tree | 4ac2eb2e2410b6c7ed72cefde5f914712474c69f /activerecord | |
parent | 79c47abe6ce0bdcc81c35aa30da8a05c3650d04d (diff) | |
download | rails-bf9a0ae12b701cba7a8aac2955ce243866ac7bf6.tar.gz rails-bf9a0ae12b701cba7a8aac2955ce243866ac7bf6.tar.bz2 rails-bf9a0ae12b701cba7a8aac2955ce243866ac7bf6.zip |
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>
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/method_scoping_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/models/developer.rb | 5 |
3 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 83f0b58e8a..c1c49c3d84 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -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 diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 1081aa40a9..3151457440 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -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 diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index e7a1e110d7..e35de3b9b0 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -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
\ No newline at end of file |