“נְקֹם, נִקְמַת בְּנֵי יִשְׂרָאֵל, מֵאֵת, הַמִּדְיָנִים; אַחַר, תֵּאָסֵף אֶל-עַמֶּיךָ”
‘Avenge the children of Israel of the Midianites; afterward shalt thou be gathered unto thy people.’”
Before Moses leves the world, God gave him the opportunity to punish the Midianites for what they did to the Israelites. It was Moses’s task because the Midianites wanted to hurt him. Moab was left without punishment at this time by Moses because God waited until Naomi and Ruth left them so Ruth can bring to the world the seed of Mashiach, King David. For that reason, Moab was punished at the time of King David.
“מוֹאָב, סִיר רַחְצִי–עַל-אֱדוֹם, אַשְׁלִיךְ נַעֲלִי עָלַי, פְּלֶשֶׁת הִתְרוֹעָעִי”
“Moab is my washpot; upon Edom do I cast my shoe; Philistia, cry aloud because of me!”
About four hundred years later King David washed the Israelites’ vessel from the impurity that Moab inflicted on them.
Even after many generations, the Midianites kept going after the Israelites. When they saw that Joshua and other righteous leaders passed away, they believed that it’s a good time to take over the Israelites.
“וְהָיָה אִם זָרַע יִשְׂרָאֵל וְעָלָה מִדְיָן וַעֲמָלֵק וּבְנֵי קֶדֶם וְעָלוּ עָלָיו”
“And so it was, when Israel had sown, and the Midianites came up, and the Amalekites, and the children of the east; they came up against them;”
They went to the Amalekites and other nations of the east and joined forces to attack Israel. When the Israelites did evil, God delivered then into the hands of the Midianites for seven years (Judges 6:1).