我有以下的ActiveRecord型号:HAS_ONE协会不工作
class User < ActiveRecord::Base
has_one :faculty
end
class Faculty < ActiveRecord::Base
belongs_to :head, class_name: 'User', foreign_key: :user_id
end
当我尝试使用这种关联来拉用户与faculty.head
我得到无差错的记录,但是当我键入user.faculty
我得到一个错误:
Faculty Load (1.8ms) SELECT "faculties".* FROM "faculties" WHERE "faculties"."user_id" = 1 LIMIT 1
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying = integer
LINE 1: ...".* FROM "faculties" WHERE "faculties"."user_id" = 1 LIMIT ...
我faculties
分贝模式是这样的:
create_table "faculties", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "user_id"
end
这是一些PG错误吗?
你的院系表的模式是什么? –
奇怪!请尝试belongs_to:head,class_name:'User',foreign_key:'user_id' –
@BachanSmruty它应该如何解决问题?问题在于'has_one:faculty'关联。 –