I've got to add like 25000 records to database at once in Rails. I have to validate them, too.
Here is what i have for now:
# controller create action
def create
emails = params[:emails][:list].split("\r\n")
@created_count = 0
@rejected_count = 0
inserts = []
emails.each do |email|
@email = Email.new(:email => email)
if @email.valid?
@created_count += 1
inserts.push "('#{email}', '#{Date.today}', '#{Date.today}')"
else
@rejected_count += 1
end
end
return if emails.empty?
sql = "INSERT INTO `emails` (`email`, `updated_at`, `created_at`) VALUES #{inserts.join(", ")}"
Email.connection.execute(sql) unless inserts.empty?
redirect_to new_email_path, :notice => "Successfuly created #{@created_count} emails, rejected #{@rejected_count}"
end
It's VERY slow now, no way to add such number of records 'cause of timeout.
Any ideas? I'm using mysql.