Ramban (Netzovim 29:17) writes that anyone whose heart is with G-d will not have an idolatrous child. Is he saying that anyone who is righteous will have righteous offspring?
What about Yishai who never sinned, but his son David did sin?
See also King Yosam son of Uzziah described in Succah 45b. He had a wicked son Achaz. Avraham had Yishamel, Yitzchak had Eisav?
The Ramban does sound like anyone who has a child who rejects Hashem as Jews know him and accepts some form of substitute for Hashem, should look into their own heart for blasphemy. We could have accepted that notion and always question the commitment of the parents, but you raised some questions about people we consider righteous and find it difficult to question their commitment to the true nature of Hashem. His perfection, intrinsic imperativeness, simplicity, independence and unity were surely beyond doubt in the inds of Yishai, Avrohom and Yitzchak. How could these people that we would never suggest are deficient in their belief in Hashem’s nature have fathered children that are deficient in this area? The question about King David can be mitigated were we to delve into the nature of his sin. But the point is still well taken.
The Ramban himself alludes to this problem by making reference to the section of Yechezkel that describes a tzaddik who had an evil son. His response is mysterious and says that it happens and it is a secret why it happens. In effect, you are asking for dinonline.org to reveal the secret which the Ramban would not share.
Well, I don’t know the answer, but can direct you to the Kabbalist Recanti who offers some insight. The Recanti also says he doesn’t understand the Ramban and also uses the example of Avrohom and Yitzchak who had children that were evil. Recanti then seems to suggest that the Ramban is not referring to all biological offspring. He makes reference to a concept that one can be a biological child but the spiritual product of a different man. If an evil child is born to a righteous person, the spiritual father of the evil child is someone other than the biological father. This esoteric idea was purposely hidden by the Ramban. The Recanti was puzzled by the Ramban and was seeking some clarity and offered a direction to his readers. Perhaps this notion will spur you into researching this Kabbalistic section of the Ramban when you feel you are ready for it. It certainly sounds fascinating. Let us know if you found out more that you can share with us. Meanwhile, I shall continue learning and sharing the revealed Torah.
 ריקאנטי דברים פרק כט פסוק יד:
וטעם פורה ראש ולענה [שם יז] פירש הרב הגדול [הרמב”ן] זה לשונו, לרמוז כי משורש מתוק לא יצא מר, וכל אשר לבבו שלם עם השם הנכבד ולא הרהר כלל בעבודה זרה לא יוליד מודה בה. ואל תקשה עלי בזה מן הכתוב [יחזקאל יח, י] והוליד בן פריץ, כי אמת הדבר וסוד גדול ולא אוכל לפרש, עד כאן דבריו ז”ל. ואני שמעתי ולא אבין, ואולי כי כוונת הרב ז”ל היא כי אין נקראין בניו רק החדשות, והצדיק לא יוליד בן חדש רק אם יהיה צדיק כמוהו, ואם תמצא שהוא פריץ ושופך דם, דע כי הוא בן הפריץ, שזה הצדיק הולידו, ולא היה זה הצדיק אביו הראשון, והראיה שלא נקרא זרע רק הצדיקים שנאמר [בראשית כא יב] כי ביצחק יקרא לך זרע, ואמרו רבותינו ז”ל [נדרים לא ע”א] ביצחק ולא בישמעאל, ביצחק ולא כל יצחק רק ביעקב ולא בעשו.