The peculiar - and uncomplimentary - story of Judah and Tamar was meant to set the stage for King David and the future Mashiach, the messianic king. The Midrash poignantly describes the scene:
"The tribes [Jacob's sons] were occupied with selling Joseph. Joseph was occupied with his sackcloth and fasting. Reuben was occupied with his sackcloth and fasting. Jacob was occupied with his sackcloth and fasting. Judah was occupied finding a wife.And God? He was occupied creating the light of the Messiah."
But the entire story is strange. If God had already decided to bestow this wonderful honor to Judah - the Mashiach would come from his descendants! - then why couldn't Judah have found a wife in a more respectable manner?
The Matching Gift
The Maggid told the story of a wealthy man married off his son. The local rabbi sent a congratulatory letter. The letter included a beautiful poem, written in exquisite calligraphy, extolling the virtues of the bride and groom and their respective families.
The letter and the poem were truly wonderful - but it was inscribed on an worn-out piece of scrap paper with uneven edges. The father of the groom was upset when he saw this peculiar gift. However, he decided not to ruin the joy of the moment; he would respond to the slight on a later date.
After the wedding, the wealthy man sent the rabbi a basket of fresh fruits and delicious cakes. But he placed the fruits and cakes inside a grimy bowl, and covered it with a soiled napkin. He found a bedraggled street urchin to deliver the gift.
The rabbi was amazed by this strange gift. When he met the wealthy man a few days later, he asked him, "Why did you do this? Why did you send me such an expensive gift wrapped in rags?"
The wealthy man replied, "I was only repaying you in kind. You also sent me a beautiful gift in an ugly, unattractive vessel."
The Midrash teaches that Joseph was destined to go down to Egypt in order to fulfill the "profound counsel" of God's covenant with Abraham, the Brit Bein HaBetarim. "They will enslave and persecute them." This was all part of the Divine plan to create the Jewish people.
There were many ways that God could have brought Joseph down to Egypt. Since the brothers hated Joseph and sought to disprove his dreams of grandeur, God let them be the cause for Joseph's descent. The final result of the sale of Joseph - which was Judah's idea - was desirable; but the way it was done, with betrayal and lies to their father, was obviously improper and unseemly.
The Midrash teaches that the affair of Tamar was a case of poetic justice, middah knegged middah. With this incident, God was telling Judah:
"You tricked your father with a goat; so too, Tamar will trick you with a goat.
You asked your father, "Do you recognize this?" Tamar will also ask you, "Do you recognize this?"
The ultimate result of Joseph's sale was good; but on the outside it looked ugly and repulsive. God therefore repaid Judah in the same coin. He gave Judah a wonderful gift, but in a soiled and tattered wrapping. The final result was priceless - preparing the way for King David and the Mashiach - but it came in a disreputable vessel, in a way that suggested prostitution and moral turpitude.