From 1d618455870cac504256cd762cd0787c739dfe1f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 24 Jan 2005 11:20:47 +0000 Subject: Fixed that records fetched with piggy-back attributes or through rich has_and_belongs_to_many associations couldn't be saved due to the extra attributes not part of the table #522 [Eric Anderson] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@483 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 3 ++- activerecord/test/base_test.rb | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 81083fc24c..2595960978 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that records fetched with piggy-back attributes or through rich has_and_belongs_to_many associations couldn't be saved due to the extra attributes not part of the table #522 [Eric Anderson] + * Added mass-assignment protection for the inheritance column -- regardless of a custom column is used or not * Fixed that association proxies would fail === tests like PremiumSubscription === @account.subscription diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8ae636afbb..ede60e3f6d 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1115,10 +1115,11 @@ module ActiveRecord #:nodoc: # an SQL statement. def attributes_with_quotes(include_primary_key = true) columns_hash = self.class.columns_hash - @attributes.inject({}) do |attrs_quoted, pair| + attrs_quoted = @attributes.inject({}) do |attrs_quoted, pair| attrs_quoted[pair.first] = quote(pair.last, columns_hash[pair.first]) unless !include_primary_key && pair.first == self.class.primary_key attrs_quoted end + attrs_quoted.delete_if { | key, value | !self.class.columns_hash.keys.include?(key) } end # Quote strings appropriately for SQL statements. diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index c5a6b7d656..f6d7e31683 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -114,7 +114,14 @@ class BasicsTest < Test::Unit::TestCase topicReloaded = Topic.find(id) assert_equal("New Topic", topicReloaded.title) end - + + def test_create_columns_not_equal_attributes + topic = Topic.new + topic.title = 'Another New Topic' + topic.send :write_attribute, 'does_not_exist', 'test' + assert_nothing_raised { topic.save } + end + def test_create_through_factory topic = Topic.create("title" => "New Topic") topicReloaded = Topic.find(topic.id) @@ -140,6 +147,18 @@ class BasicsTest < Test::Unit::TestCase assert_equal("Updated topic", topicReloadedAgain.title) end + def test_update_columns_not_equal_attributes + topic = Topic.new + topic.title = "Still another topic" + topic.save + id = topic.id + + topicReloaded = Topic.find(id) + topicReloaded.title = "A New Topic" + topicReloaded.send :write_attribute, 'does_not_exist', 'test' + assert_nothing_raised { topicReloaded.save } + end + def test_preserving_date_objects # SQL Server doesn't have a separate column type just for dates, so all are returned as time if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter -- cgit v1.2.3