How would you choose between Belongs_to And Has_one?

Technology CommunityCategory: Ruby on RailsHow would you choose between Belongs_to And Has_one?
VietMX Staff asked 3 years ago
Problem

If you want to set up a one-to-one relationship between two models, you’ll need to add belongs_to to one, and has_one to the other. How do you know which is which?

The distinction is in where you place the foreign key (it goes on the table for the class declaring the belongs_to association), but you should give some thought to the actual meaning of the data as well.

The has_one relationship says that one of something is yours – that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier.