A major thunderstorm hits Springfield, and Marge demands that Homer fix their leaking roof. Homer attempts to solve the problem by using Hot Wheels ramps to transport all the draining water from the roof to the front yard through the hallway, the stairway and the mail slot on the front door. Though this plan seems to work well, Lisa's hamster slides down the ramps to the front yard by accident. Shocked, Lisa opens the front door to save her hamster, breaking all the ramps, and leaving the house all wet. Marge berates Homer for not providing a more sensible solution to fix the roof. Angered, Homer decides to go to Moe's Tavern, but is kicked out when he sits on Lenny's birthday cake by accident, shaped in the form of Lenny's favourite bar stool. Feeling depressed, Homer finds another bar, "Knockers" (A parody of the restaurant Hooters), where he meets a friendly man named Ray Magini. The two talk, and Homer finds out that Ray is a roofer, so Homer asks him to fix the leak in his roof. Ray agrees.
The next day, Homer assures Marge that his new friend will be taking care of the roof. Ray, however, does not arrive until everyone else has left. The two of them get up on the roof and use nail guns to nail the boards onto the roof, but they start shooting nails at each other, some hitting Ned Flanders' lawn mower next door. Ray leaves later and, as Homer runs on the roof, he crashes through the small part of the roof that they fixed, making the hole bigger. Meanwhile, Marge and the kids leave Santa's Little Helper with Grandpa and the Springfield Retirement Home residents, because they seem to like him. When Marge sees the hole, she tells Homer to fix it himself, because she sees no reason to believe that his friend will.
The next day, Bart and Homer go to the Builder's Barn, and Homer meets Ray there. Ray apologizes to Homer for not having finished the job and promises that he will stop by soon to work on the roof. Lisa, Marge, and Maggie arrive back at the retirement home, only to discover that Santa's Little Helper has become one of the old people. They permanently take him back home. After waiting a long while for Ray to show, Marge becomes worried about Homer, and tells him that Ray is just a figment of his imagination, though Homer refuses to believe it.
Marge then takes him to the Calmwood Mental Hospital. Dr. Hibbert tells Homer that Ray does not exist; he was created by Homer's mind as Homer was feeling lonely and unappreciated because of the previous events he endured. All the people that Homer thinks saw Ray (Bart, Flanders, and the "Knockers" bartender) claim they did not, and Lisa reveals that "Ray Magini" is an anagram for "imaginary". Seven weeks and several hours of shock therapy later, Homer is now sure that Ray does not exist. As he is being discharged, he sees Ray again in the room. Angered by the pain the "figment of his imagination" has caused him, he assaults him. In retaliation, Ray knocks Homer out and everyone is surprised they can see him too.
He did really exist the whole time: the bartender did not see Ray as he had an eye patch on (while looking in the direction of Homer and Ray that night, Ray was before his covered eye), and Ray could not be seen by Flanders because he was behind the chimney. Bart still viewed Homer with skepticism for talking to thin air, but Stephen Hawking arrives and says that Bart could not see Ray at the hardware store because of a miniature black hole caused directly behind Ray which absorbed the light from Ray and made it look as though Homer was talking to himself when Homer was actually talking to Ray (A theory that just covers up a plot hole). Marge asks Ray why he started fixing the roof, and then just disappeared. Ray says he is a contractor. Everyone laughs, and Marge says "That's right, you're all crooks!" Dr. Hibbert, seeing how angry Homer is as he was made to go through shock treatment for nothing, offers to make it up to him by doing a free eye scraping for him. Homer agrees on it, but also forces Hibbert to fix the roof without any breaks while Ray and Homer discuss Everybody Loves Raymond on the roof.