I have a rails table. (normalusers)=> id, parent_id, Height. I have added the height column afterward. Now I want to add value to it. Is there any way I can achieve this? I have tried migration but it is not updating heights. I think functions do not work in migration. After migration, all the rows have the value of height same as before.
class Latest < ActiveRecord::Migration[6.0]
def change
i = 1
loop do
i += 1
puts i
record=Normaluser.find(i)
record.height=get_height(record)
record.save!
if i == 39
break # this will cause execution to exit the loop
end
end
end
def get_height(record)
maxHeight=0
Normaluser.where(pid: record.id).each do |f|
tempHeight=get_height(f)
if tempHeight > maxHeight
maxHeight=tempHeight
end
end
return maxHeight
end
end
get_heightfunction is correct.putsstatement when you run the migration.