Skip to content

Commit

Permalink
Fixed that records fetched with piggy-back attributes or through rich…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
dhh committed Jan 24, 2005
1 parent 3d1e8dd commit 1d61845
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *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 * 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 * Fixed that association proxies would fail === tests like PremiumSubscription === @account.subscription
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -1115,10 +1115,11 @@ def attributes_protected_by_default
# an SQL statement. # an SQL statement.
def attributes_with_quotes(include_primary_key = true) def attributes_with_quotes(include_primary_key = true)
columns_hash = self.class.columns_hash 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[pair.first] = quote(pair.last, columns_hash[pair.first]) unless !include_primary_key && pair.first == self.class.primary_key
attrs_quoted attrs_quoted
end end
attrs_quoted.delete_if { | key, value | !self.class.columns_hash.keys.include?(key) }
end end


# Quote strings appropriately for SQL statements. # Quote strings appropriately for SQL statements.
Expand Down
21 changes: 20 additions & 1 deletion activerecord/test/base_test.rb
Expand Up @@ -114,7 +114,14 @@ def test_create
topicReloaded = Topic.find(id) topicReloaded = Topic.find(id)
assert_equal("New Topic", topicReloaded.title) assert_equal("New Topic", topicReloaded.title)
end 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 def test_create_through_factory
topic = Topic.create("title" => "New Topic") topic = Topic.create("title" => "New Topic")
topicReloaded = Topic.find(topic.id) topicReloaded = Topic.find(topic.id)
Expand All @@ -140,6 +147,18 @@ def test_update
assert_equal("Updated topic", topicReloadedAgain.title) assert_equal("Updated topic", topicReloadedAgain.title)
end 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 def test_preserving_date_objects
# SQL Server doesn't have a separate column type just for dates, so all are returned as time # SQL Server doesn't have a separate column type just for dates, so all are returned as time
if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter
Expand Down

0 comments on commit 1d61845

Please sign in to comment.