[rails] modifying column data inside migration
image you want to add a column which data depends on other data previously on the table like:
add_column :tables, :new_row, :string
Table.all.each do |table|
if table.correct?
table.new_row = 'yes'
else
table.new_row = 'no'
end
end
That will not work :(
You would need to tell that model to reload its table scheme information first.
add_column :tables, :new_row, :string
Table.reset_column_information
Table.all.each do |table|
if table.correct?
table.new_row = 'yes'
else
table.new_row = 'no'
end
end
Written by Miguel
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#