  We improve the bound of the $g$-invariant of the ring of integers of a
totally real number field, where the $g$-invariant $g(r)$ is the smallest
number of squares of linear forms in $r$ variables that is required to
represent all the quadratic forms of rank $r$ that are representable by the sum
of squares. Specifically, we prove that the $g_{\mathcal{O}_K}(r)$ of the ring
of integers $\mathcal{O}_K$ of a totally real number field $K$ is at most
$g_{\mathbb{Z}}([K:\mathbb{Q}]r)$. Moreover, it can also be bounded by
$g_{\mathcal{O}_F}([K:F]r+1)$ for any subfield $F$ of $K$. This yields a
sub-exponential upper bound for $g(r)$ of each ring of integers (even if the
class number is not $1$). Further, we obtain a more general inequality for the
lattice version $G(r)$ of the invariant and apply it to determine the value of
$G(2)$ for all but one real quadratic field.
