I’ve heard that one who spends time in the path of Allah, his seven generations will be saved from misguidance.
I haven’t seen this in any Hadith of Rasulullah (sallallahu’alayhi wasallam).
However under the verse of Surah Kahf: “And their father was pious” (Ayah: 82) the commentators mention that Allah Ta’ala had protected the wealth of the 2 orphans due to the piety of their father. Some have stated that it was actually their forefather from 7 generations before who was the pious one.
(Tafsirul Baghawi, Tafsir Ruhul Ma’ani, Tafsir Abus Sa’ud, Tafsir Sawi and others, Surah Kahf, verse: 82)
After quoting this last view from Sayyiduna Ja’far As-Sawdiq (rahimahullah) Al-Mufassir Alusy (rahimahullah) writes: “Whatever it may be, this still proves that the piety of the parents does result in protection for their progenies. Imams Ibn Abi Shaybah, Ahmad (in his kitabuz zuhd) and Ibn Abi Hatim (rahimahumullah) have reported from Khaythamah that he quoted Nabi ‘Isa (‘alayhis salam) to have said:
“How very excellent is the matter of the progeny of a (true) believer, how well they are protected after him (i.e, after his demise)”
The narrator: Khaythamah then recited the above verse of Surah Kahf.
(Tafsir Ruhul Ma’ani, Surah Kahf, verse: 82)
Imam Baghawi (rahimahullah) writes: “It is said they were 7 generations below their pious forefather. Muhammad ibnul Munkadir says: “Indeed because of the piety of a man, Allah protects his children and grandchildren”
(Tafsir Baghawi, Surah Kahf, verse: 82)
Hafiz Ibn Kathir (rahimahullah) while commentating on the above verse writes: “This proves that the progeny of a pious man is divinely protected.”
There is more support for the above principle in other verses of the Quran and the tafsir thereof. (see Surah: 40 Ayah: 8 and Surah: 52, Ayah: 21 and their respective commentaries)
It is possible somebody made the above statement with this in mind that when one spends time serving Allah’s din (Islam) one acquires the attributes of piety and therefore his progeny will benefit too.
It should be noted that this virtue is not only for those mentioned in your query. It applies to all the pious.
And Allah Ta’ala Knows best,
Answered by: Moulana Muhammad Abasoomar