Ruby on Rails Saturday, October 30, 2010

I need to change the type of a column (wasserstand) from string to
integer, which already contains text.

My idea is to copy the string into a new scratch column, modify the
column type to integer and then manually set the appropriate ints.

After this, I want to remove the scratch string column in the next
migration.

The first migration should be done like this:

def self.up
tz = Teilzaehlung.all
ws = []
tz.each do |b|
ws << [b.id, b.wasserstand]
end

change_column(:teilzaehlungs, :wasserstand, :integer)
add_column(:teilzaehlungs, :wst, :string)

ws.each do |b|
beob = Teilzaehlung.find(b[0])
beob.update_attribute(:wasserstand, 0)
beob.update_attribute(:wst, b[1])
end
end

Interestingly, after running the migration, the new interger typed
column contains 0, but the scratch column wst is always NULL - even if
b[1] contains a string.

Why?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment