class NormalizeNetworkSupplierWebsites < ActiveRecord::Migration class NetworkSuppliersTable < ActiveRecord::Base self.table_name = "network_suppliers" end def up NetworkSuppliersTable.where("website IS NOT NULL").find_each do |record| record.website = record.website.to_s.split.grep(UrlValidator::REGULAR_EXPRESSION).first # The first valid website or nil record.save! if record.changed? end end def down raise ActiveRecord::IrreversibleMigration end end