Every quote is from Young's Literal Translation.
Deuteronomy 23:3 did not allow Ammonites or Moabites to enter God's House:
'An Ammonite and a Moabite doth not enter into the assembly of Jehovah; even a tenth generation of them doth not enter into the assembly of Jehovah -- to the age;
Yet 2Samuel 7:18 says David went before God:
And king David cometh in and sitteth before Jehovah, and saith, 'Who am I, Lord Jehovah? and what my house, that Thou hast brought me hitherto?
Numbers 21:21-35 shows how the Israelites took the land of the Moabites from the Amorites - who had taken it from the Moabites:
And Israel sendeth messengers unto Sihon king of the Amorite, saying,
'Let me pass through thy land, we do not turn aside into a field, or into a vineyard, we do not drink waters of a well; in the king's way we go, till that we pass over thy border.'
And Sihon hath not suffered Israel to pass through his border, and Sihon gathereth all his people, and cometh out to meet Israel into the wilderness, and cometh in to Jahaz, and fighteth against Israel.
And Israel smiteth him by the mouth of the sword, and possesseth his land from Arnon unto Jabbok -- unto the sons of Ammon; for the border of the sons of Ammon is strong.
And Israel taketh all these cities, and Israel dwelleth in all the cities of the Amorite, in Heshbon, and in all its villages;
for Heshbon is a city of Sihon king of the Amorite, and he hath fought against the former king of Moab, and taketh all his land out of his hand, unto Arnon;
therefore those using similes say -- 'Enter ye Heshbon, Let the city of Sihon be built and ready,
For fire hath gone out from Heshbon, A flame from the city of Sihon, It hath consumed Ar of Moab, Owners of the high places of Arnon.
Wo to thee, O Moab, Thou hast perished, O people of Chemosh, He hath given his sons who escape -- Also his daughters -- Into captivity, to a king of the Amorite -- Sihon!
And we shoot them, Perished hath Heshbon unto Dibon, And we make desolate unto Nophah, Which is unto Medeba.'
And Israel dwelleth in the land of the Amorite [formerly the land of the Moab],
and Moses sendeth to spy out Jaazer, and they capture its villages, and dispossess the Amorite who is there,
and turn and go up the way of Bashan, and Og king of Bashan cometh out to meet them, he and all his people, to battle, at Edrei.
And Jehovah saith unto Moses, 'Fear him not, for into thy hand I have given him, and all his people, and his land, and thou hast done to him as thou hast done to Sihon king of the Amorite, who is dwelling in Heshbon.'
And they smite him, and his sons, and all his people, until he hath not left to him a remnant, and they possess his land.
300ish years later, the Ammonites who bordered this land tried to claim that this land Israel had taken belong to them and they wanted it back.
And Jephthah sendeth messengers unto the king of the Bene-Ammon, saying, 'What -- to me and to thee, that thou hast come in unto me, to fight in my land.'
And the king of the Bene-Ammon saith unto the messengers of Jephthah, 'Because Israel took my land in his coming up out of Egypt, from Arnon, and unto the Jabbok, and unto the Jordan; and now, restore them in peace.'
And Jephthah addeth yet and sendeth messengers unto the king of the Bene-Ammon,
and saith to him, 'Thus said Jephthah, Israel took not the land of Moab [that is, they did not take it from Moab], and the land of the Bene-Ammon,
for in their coming up out of Egypt, Israel goeth in the wilderness unto the Red Sea, and cometh in to Kadesh,
and Israel sendeth messengers unto the king of Edom, saying, Let me pass over, I pray thee, through thy land, and the king of Edom hearkened not; and also unto the king of Moab hath Israel sent, and he hath not been willing; and Israel abideth in Kadesh [Israel did not engage Edom or Moab],
and he goeth through the wilderness, and compasseth the land of Edom and the land of Moab, and cometh in at the rising of the sun of the land of Moab, and they encamp beyond Arnon, and have not come into the border of Moab, for Arnon is the border of Moab. [Israel went around Edom and Moab]
'And Israel sendeth messengers unto Sihon, king of the Amorite, king of Heshbon, and Israel saith to him, Let us pass over, we pray thee, through thy land, unto my place,
and Sihon hath not trusted Israel to pass over through his border, and Sihon gathereth all his people, and they encamp in Jahaz, and fight with Israel;
and Jehovah, God of Israel, giveth Sihon and all his people into the hand of Israel, and they smite them, and Israel possesseth all the land of the Amorite [including the land that formerly belong to Moab], the inhabitant of that land,
and they possess all the border of the Amorite from Arnon, and unto the Jabbok, and from the wilderness, and unto the Jordan.
'And now, Jehovah, God of Israel, hath dispossessed the Amorite from the presence of His people Israel, and thou wouldst possess it!
That which Chemosh thy god causeth thee to possess -- dost thou not possess it? and all that which Jehovah our God hath dispossessed from our presence, -- it we do possess.
'And now, art thou at all better than Balak son of Zippor, king of Moab? did he at all strive with Israel? did he at all fight against them?
In Israel's dwelling in Heshbon and in its towns, and in Aroer and in its towns, and in all the cities which are by the sides of Arnon three hundred years -- and wherefore have ye not delivered them in that time?
And I -- I have not sinned against thee, and thou art doing with me evil -- to fight against me. Jehovah, the Judge, doth judge to-day between the sons of Israel and the sons of Ammon.'
This land changed owners twice, but it kept the name of the original owners - Moab. The people who lived in this "land of Moab" in Ruth's time were full-blooded Israelites. Ruth was a full-blooded Israelite. David's great-grandmother was a full-blooded Israelite.
And it cometh to pass, in the days of the judging of the judges, that there is a famine in the land, and there goeth a man from Beth-Lehem-Judah to sojourn in the fields of Moab, he, and his wife, and his two sons.
And the name of the man is Elimelech, and the name of his wife Naomi, and the name of his two sons Mahlon and Chilion, Ephrathites from Beth-Lehem-Judah; and they come into the fields of Moab, and are there.
And Elimelech husband of Naomi dieth, and she is left, she and her two sons;
and they take to them wives, Moabitesses [from the land the was formerly Moab and now under Israelite control - not the people of Moab]: the name of the one is Orpah, and the name of the second Ruth; and they dwell there about ten years.
This will not seem so strange when we consider the how Israel was known as the land of Canaan even after Israel took it over. Israelites lived there - it was just still known by its old name.