What's the difference between belongs_to and has_one?

前端 未结 5 493
有刺的猬
有刺的猬 2020-11-28 02:04

What is the difference between a belongs_to and a has_one?

Reading the Ruby on Rails guide hasn\'t helped me.

5条回答
  •  生来不讨喜
    2020-11-28 02:44

    From a simplicity standpoint, belongs_to is better than has_one because in has_one, you would have to add the following constraints to the model and table that has the foreign key to enforce the has_one relationship:

    • validates :foreign_key, presence: true, uniqueness: true
    • add a database unique index on the foreign key.

提交回复
热议问题