the octet rule states that atoms will gain, lose, or share electrons until they are surrounded by eight valence electrons to form a molecule. yes it applies to ions in an ionic compound the cation has lost electrons and the anion has gained electrons for example in MgCl2, Mg loses 2 electrons to become Mg+2 and each Cl atom gains 1 electron to form Cl -1 which forms Mg+2Cl2-2