Taking pity on the hapless Ralph Wiggum, Marge pairs him with Bart, but his association with Ralph quickly turns out to be more beneficial than burdensome when Bart learns that his father holds the master key to every store in Springfield. Late Phil Hartman guest stars.
A remote controlled robot informs the students of Springfield Elementary School about the newly opened Springfield Knowledgeum, where the family decides to go to later. There, Bart wanders off and walks into an exhibit of the planet Mars. There he sees Ralph Wiggum, who is in the process of being pushed into a giant ear by Nelson and his gang of bullies. When Ralph is freed by a center employee, Marge and Chief Wiggum are there to meet him. Marge observes that Ralph has a vivid imagination and realizes that he has no friends to play with; she arranges for Ralph to spend time with Bart.
Bart is unhappy with the arrangement, but later exploits it when he discovers Ralph's father has a police master key capable of opening any door in Springfield. Bart and Ralph thus steal the key and raid several shops. After encountering Nelson and his gang, the boys go to the unused Morningwood penitentiary. When Ralph objects because he is afraid, Bart takes the key and intends to go on without him. However, the bullies turn against him, and Bart is made to feel how he made Ralph feel ("I hope the irony isn't lost on you Simpson," Nelson remarks). Bored with them, the bullies leave - but not before tossing the key into the penitentiary. Ralph and Bart go in after it, in the process activating an old electric chair to melt a wedding cake doll.
Afterwards at the Simpson home, Bart and Ralph learn the penitentiary will once again be used by the town. Unaware that the power is active, Mayor Quimby straps himself into the electric chair in a publicity attempt. Unable to warn Mayor Quimby by telephone that he may be electrocuted, Ralph points out to Bart that Lisa can probably figure out a way to warn the Mayor. She decides to launch a small model rocket with a warning message attached and aims it toward the penitentiary. The rocket is blown off-course and crashes through Mr. Burns' office window. Mr. Burns reads the note, and realizes that the penitentiary has been receiving free power for over thirty years; he responds to it by shutting off the penitentiary's power. When the power goes out, the mayor is in the middle of being electrocuted, but survives. In the aftermath, the Simpsons congratulate Ralph, pointing out that even Lisa could not solve the problem. Lisa is at first baffled that she received no credit for having thought of the solution and implementation, but she is reminded by Bart that it is rare for Ralph to be useful or praised, and that he needs a little credit. Lisa then joins in the praising. As Ralph enjoys the praise, his imaginary friend the Leprechaun (who he mentions to Bart when visiting Ralph's home) appears on his shoulder and congratulates him, before telling Ralph to burn the house down and everyone inside it. Wearing his trademark vacant smile, Ralph nods in agreement.