Forum: Ruby on Rails has_one

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jonas (Guest)
on 2007-06-30 17:00
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])

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?
Thorsten (Guest)
on 2007-06-30 17:16
(Received via mailing list)
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.
This topic is locked and can not be replied to.