【发布时间】:2025-12-19 23:45:07
【问题描述】:
在我的 Rails 4 应用程序中,我的目标是查看所有联系人,其中联系人表中的字段 visible_to 等于 1。我的 visible_to 是 :integer, array: true。
但是,我得到以下异常:
PG::UndefinedFunction: ERROR: operator does not exist: integer[] = integer
LINE 1: ....* FROM "contacts" WHERE "contacts"."visible_to" IN (1) OR...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.: SELECT "contacts".* FROM "contacts" WHERE "contacts"."visible_to" IN (1) ORDER BY created_at DESC
我搜索了答案,据我所知,visible_to 类型存在问题。但是,我找不到解决方案。我也尝试从演员表提示中获益,但徒劳无功。
我的迁移:
class AddVisibleToToContacts < ActiveRecord::Migration
def change
add_column :contacts, :visible_to, :integer, array: true, default: [], using: 'gin'
end
end
来自控制器的相关变量:
@contacts_all_user_sorted = Contact.all.where(visible_to: [1]).order("created_at DESC")
【问题讨论】:
-
嗨。您在迁移文件中写“默认”而不是“默认”是否正常?
-
谢谢,这里只是一个错字。在迁移中一切都很好。
-
我对问题原因的简短初步汇报。 (1) 一次又一次 RTFM,不仅是 ActiveRecord,还有PostgreSQL (2) 我太依赖类似的问题和similar solution,这与我无关(因为 的 PostgreSQL 具体的?)。
标签: ruby-on-rails postgresql activerecord