Rails:使用不同的地方

Mat*_*ias 2 activerecord ruby-on-rails distinct

我有一个 ActiveRecord 查询:

Shareholder.where(is_company: false).distinct
Run Code Online (Sandbox Code Playgroud)

这给了我错误:

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  could not identify an equality operator for type json
Run Code Online (Sandbox Code Playgroud)

ActiveRecord 创建的 SQL 查询: SELECT DISTINCT "shareholders".* FROM "shareholders" WHERE "shareholders"."deleted" = $1 AND "shareholders"."is_company" = $2

我有点困惑这行不通。怎么了?

Ele*_*yan 5

Distinct 不适用于 json 列,但您可以试试这个

Shareholder.select("DISTINCT ON (shareholders.id) shareholders.*").where(is_company: false).distinct
Run Code Online (Sandbox Code Playgroud)