Forum: Ruby on Rails has_one

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cc24d46043aac77ee0cba0ad2c2d51f1?d=identicon&s=25 Jonas (Guest)
on 2007-06-30 15: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])
@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?
D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2007-06-30 15: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.