With the profile's belongs_to :user
association, if you want the association to be available the next time the object is loaded from the database, you'd better store the user_id
in the profiles
table. There's really no other way. However, you certainly don't need profile_id
column for the opposite association. Rails is smart enough to look in the profiles
table when interpreting the User
's has_one :profile
association.
That said, in general the cost of a single integer database column is nothing to worry about. In this case, you don't need the integer column, but consider that the user's email address will be several times larger in terms of bytes of storage and index size.
There are some disadvantages to having the profile_id
column, by the way. Rails, to my knowledge, wouldn't even update the column automatically if you reassign the profile
attribute on the a User
. That behavior is particular to a belongs_to
association.
Moreover, storing redundant information in a relational database is asking for trouble, since it opens up possibility of a contraction. If user 1 points to profile 1, but profile 1 points to user 2, how do you interpret that? Of two redundant facts, which one do you trust? And if one fact is always trusted over another, what's the point of storing the second fact at all? This is sort of tangential to your question, but I thought it was worth mentioning nonetheless.