So, I have a user model:
class User < ActiveRecord::Base
has_one :partner, class_name: "User", foreign_key: :id
belongs_to :inverse_partner, class_name: "User", foreign_key: :id
end
And each user has an id (prepared by the scaffold), a partner_id and a inverse_partner_id column in the users table.
However, when I try to to do this:
aka[x].partner = shiro[x]
Where both aka[x] and shiro[x] are Users, I get this weird error where the SQL statement indicates that I'm setting id and not partner_id as I intended to:
SQLite3::MismatchException: datatype mismatch: UPDATE "users" SET "id" = ?, "updated_at" = ? WHERE "users"."id" = 4
I'm obviously doing something very wrong. Help?
The only required change I see is in your associations'
foreign_keyoption.With the current setup you are basically using one single column for three different purposes i.e. primary key for
userstable, foreign key forpartnerassociation and foreign key forinverse_partnerassociation. So, when this is done and you try to set partner such errors can be expected.Set appropriate foreign keys for each relations as follows: