Has_one


#1

I got two models:

Extension and User. User has a foreign key called extension_id.
Extension has not key to user since it can be used by various models.

My user model has “has_one :extension”

Why do the following code fail?
@user = User.find(params[:id])
@user.extension.create

Error:
PGError: ERROR: column extensions.user_id does not exist
RAD 1: SELECT * FROM extensions WHERE (extensions.user_id = 1) LIM…
^
: SELECT * FROM extensions WHERE (extensions.user_id = 1) LIMIT 1


I do not understand why extension have to have a user_id? Can’t
relations be one way?


#2

If the User Model has the foreign key “extension_id” it’s not
has_on :extension
it has to be
belongs_to :extension

foreign keys are always on the belongs_to side. has_one/has_many has
no foreign keys.