Hey Gustavo,
Please read the definition of Erdős number carefully.
"Erdős number one corresponds with the person who has published a paper with Erdős and Erdős number two with the one who has published a paper with the collaborator of Erdős".
Now, Hardy was a collaborator of Erdős with Erdős number of 2 and Ramanujan was the student of Hardy.
Cheers.