Permalink
Browse files

Failing test case for values not persisting after add_column

Given a migration like

  add_column :posts, :body, :string
  Post.find_each do |p|
    p.body = 'foo'
    p.save!
  end


both the assignment to 'body' and the save will succeed, but the value isn't written into the database, and you're left with nil values for 'body'.

Perhaps add_column should implicitly call reset_column_information?  Having the 'body' accessor raise NoMethodError instead of appearing to succeed would also be fine...
  • Loading branch information...
1 parent 806d023 commit f6bf26bff7c0768002e366ad33fee9df56fa8771 @jdelStrother committed Jun 11, 2012
Showing with 21 additions and 0 deletions.
  1. +21 −0 activerecord/test/cases/migration/change_schema_test.rb
@@ -311,6 +311,27 @@ def test_column_exists_on_table_with_no_options_parameter_supplied
end
end
+ def test_add_column_persists_values
+ connection.create_table :testings do |t|
+ t.column :title, :string
+ end
+ person_klass = Class.new(ActiveRecord::Base)
+ person_klass.table_name = 'testings'
+ 2.times do
+ person_klass.create!(title:'foo')
+ end
+
+ person_klass.connection.add_column 'testings', 'body', :text
+ person_klass.find_each do |person|
+ person.body = 'Ba Ba Black Sheep'
+ person.save!
+ end
+
+ person_klass.find_each do |person|
+ assert_equal 'Ba Ba Black Sheep', person.body
+ end
+ end
+
private
def testing_table_with_only_foo_attribute
connection.create_table :testings, :id => false do |t|

0 comments on commit f6bf26b

Please sign in to comment.