The Sons of Israel Corrupted by Caanites
If you touch dirt, you will get dirty. Somehow this did not stop Joseph's brother Judah from going wrong with his life. And God made a defiant point that wickedness would be punished. Judah was the one who suggested selling Joseph into slavery.
Judah, the fourth son of Jacob and Leah, married a Caanite woman called Shua. They had three sons: Er, Onan, and Shelah. The first son, Er, married Tamar but he was so evil that God allowed him to die. Onan then refused to care for Er's childless widow, and for this sin of selfishness, he also died. Shelah was too young to marry Tamar and provide her with children so Judah sent Tamar back to her father until Shelah was old enough to marry her. It seems that Judah was hesitant to allow yet another son to marry Tamar since the other sons had died. But Judah was not faithful in his duty to provide a husband for Tamar who was bound to the headship of her father-in-law Judah, and he had to give her a husband. Judah alone could determine whom and when she could marry. His failure to care for Tamar was wrong according to their culture.
Judah's wife died.
Tamar plotted to make Judah do what he was held to do -- provide her with sons to care for her.
She sat along a road she knew Judah would pass, and posed as a prostitute. He took the bait and had sex with Tamar, who then had twin sons: Perez and Zerah. This apparently was God's plan because Perez was in the line through whom King David and Jesus Christ were born. God works in mysterious ways ... the ancestral line of Jesus includes Judah's failed family.