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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Solution: Split the migration: The first creats the new column, the second processes the data. Looks like problem with migrations. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 31, 11:01 am, Fritz Trapper <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Solution: > Split the migration: The first creats the new column, the second > processes the data. > > Looks like problem with migrations. >Classes cache column information, but you can call MyClass.reset_column_information to clear that cache. Fred> -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.