Should I use has_one or belongs_to?

I have a one-to-one relationship between 2 models, should I store each others id on both objects or just in one?

Thank you,

Rodrigo

use has_one and belongs_to and i guess: store each others for maintain integrity is the best way, always

use has_one and belongs_to and i guess: store each others for maintain integrity is the best way, always

I have a one-to-one relationship between 2 models, should I store each others id on both objects or just in one?

for code readability’s sake, you need to figure out where to put the foreign but not on both. for example, you have a 1-1

relationship between a dog and an owner. it’s better if you add the foreign key to the dogs table.

Class Dog < ActiveRecord::Base

belongs_to :owner # owner_id is in the dogs table

end

Class Owner < ActiveRecord::Base

has_one :dog

end

one more thing to consider is through associations. you can’t add a :through option to belongs_to.

Well, i think that in the given example, u could put has_one :dog in
the Owner Controller and ALSO a belongs_to :owner in the Dog’s
controller. That way, you’ll have the relations of both controllers
explicit in each of them improving readability.

Well, i think that in the given example, u could put has_one :dog in

the Owner Controller and ALSO a belongs_to :owner in the Dog’s
controller. That way, you’ll have the relations of both controllers
explicit in each of them improving readability.

this is confusing. you can’t call has_one and belongs_to in a controller.

exact! this is a model task, remember guys, MVC, this is a model task, paper of model.

Oh Right! I got the names wrong. My bad. xD