Given a finitely generated projective module $M$ over a ring $R$ with exactly two idempotents $0,1$ ($X=\operatorname{Spec} R$ is connected). We have a coherent $\mathcal{O}_X$-module $\widetilde{M}$ and the rank of $\widetilde{M}$ at $\mathfrak{p}\in X$ is defined by $$\dim_{\kappa({\mathfrak{p}})}M_{\mathfrak{p}}\otimes_{R_\mathfrak{p}}\kappa({\mathfrak{p}})$$
Question.
- Why is this dimension constant?
- For an arbitrary ring $R$, why is it locally constant (is it constant on connected components)?
My attempts. Since we have $$M_{\mathfrak{p}}\otimes_{R_\mathfrak{p}}\kappa({\mathfrak{p}})\cong M\otimes_{R}R_{\mathfrak{p}}\otimes_{R_\mathfrak{p}}\kappa(\mathfrak{p})\cong M\otimes_{R}\kappa(\mathfrak{p})$$ When $R$ is an integral domain, we do have all $\kappa({\mathfrak{p}})$ are isomorphic to the fractional field at the generic point. Hence the rank is constant. Hence I think a possible generalization is, for arbitrary ring $R$, the rank of $M$ should be constant on irreducible components of $\operatorname{Spec} R$.
Updates. As KReiser pointed out, my attempt above is NOT correct. Now I am even more confused, as irreducible spaces are connected and we cannot have constant value there.
Any help is appreciated. Just in case, I have read Mr. Brandenburg's answer in this post Rank of projective module where he did not prove his claim.