I am working on a Rail webapp. I have two models, User, which contains very basic information: id, username and password, and Profile, which includes profile for each user. (The main reason is to have a lightweight User model, which will be called regularly, and a full-fledged profile which will be called irregularly). Each of these models has many children.
Right now, I have Profile with its own primary key, then a foreign key user_id to match with User.
However, I wonder if I should have Profile model with the same key as User model (i.e., Profile.id == User.id if the records refer to the same user). This is convenience because when I have an object that belongs to User, I want it to belong to Profile and vise versa. For example, I can specify User has_many and Spec has_many relationship to ChildModel. Because they use the same key, I don’t have to merge ChildModel to Spec, then Profile to User to find out user associated with child object.
The downside is in the future, if for some reason I have discrepancy between primary key of User and Spec, then I am in deep trouble.
What would be your recommendation for this situation?
Thank you.
It sounds like you should be using a one-to-one relationship between
ProfileandUser. You can create this using thehas_oneandbelongs_todeclarations.As stated in Agile Web Development with Rails Fourth Edition: