It's raining in Springfield, and Homer's roof is leaking, so Homer takes the only sensible course of action; he heads off to Moe's. However, he spoils a surprise party for Lenny, and makes it worse when the barstool he sits on turns to be a cake in the shape of Lenny's favorite seat, so he's tossed out by Moe. Homer ends up at Knockers, where he meets a fellow loner (voiced by Ray Romano), who turns out to be a roofer. However, nobody but Homer ever seems to be able to see Ray - not even Bart, who was in the home repair warehouse with the two of them - so Marge and Dr. Hibbert have Homer declared insane. 
Springfield becomes the target of a powerful thunderstorm and 742 Evergreen Terrace appears to be the main target and causes a leak in the roof. Homer plans to use a "Hot Wheels" track to stop the leak but Lisa's hamster goes down the chute and goes out the door via the mail slot and then Lisa has to open it to get her hamster and then he gets sick as he was the subject of Lisa's show and tell project. The leak also affects Bart cracks for his homework party and Maggie's teddy bear and causes Snowball to fall in quicksand which was his litter box wetted by rain. Homer then goes to Moe's Tavern but there he ruins a surprise party for Lenny (even though his 'ruining' it involved repeating what Moe said seconds before right before Lenny walks in, and sitting on a cake that looks exactly like a bar stool that's sitting on the floor not the counter) and is kicked out. Homer then goes to a bar named Knockers a parody of "Hooters" and there he meets a roofer named Ray Magini. Ray agrees to fix Homer's roof and while the kids go to visit Grandpa with Marge. Ray comes over to fix the roof, However, rather than fixing the roof the two goof offs and have a nail fight. The nails then hit Ned Flanders riding his lawn mower. Homer then asks Ned if he met Ray but Ned does not seem to know Ray. Ray then has to leave when he finds out his kid attempted "something" and Homer is on the roof all alone and then falls through the hole.
Marge then returns home to find Homer doing nothing and Homer and Marge then have a discussion about Ray and Marge simply says Ray is non-existent. Homer is then sitting on the roof waiting for Ray to return while Marge tries to get Homer to believe Ray is a figment of his imagination. Ray Magini is also an anagram of "imaginary", leading Homer to belie that Ray was indeed not real. The next day Marge sends Homer and Bart to Builders' Barn and there Homer sees Ray however Bart only sees Homer talking to himself. The next day Marge takes Homer to the Calmwood Mental Hospital and Dr. Hibbert tells Homer that Ray is a figment of his imagination he created because he was lonely and needed a friend. Homer then spends six weeks at the hospital receiving electric shock treatment. After the six weeks Marge returns with a little surprise—Ray. It turns out Ray was real. The bartender at Knockers had an eyepatch and could only see Homer's side of the table, Ray was hiding behind the roof when he saw Flanders, and a localised black hole absorbed the light between Bart and Ray preventing Bart from seeing him, explained by Stephen Hawking and as for why Ray kept disappearing is because he's a contractor. However, as punishment for making Homer spend six weeks in the mental hospital, Homer forces Dr. Hibbert to fix his roof while Homer and Ray sit on it drinking beer.
In the meantime Marge and Lisa, visit the Retirement Castle and they see how miserable the old people are and then they see how happy they are with Santa's Little Helper's company and let him stay there for a while but a day later he has become just as old and miserable as the seniors.