activerecord-hackery / ransack

Object-based searching.
https://activerecord-hackery.github.io/ransack/
MIT License
5.68k stars 805 forks source link

polymorphic search type cast issue #1079

Open zephyrpathsofglory opened 5 years ago

zephyrpathsofglory commented 5 years ago

Hi, there:

I have a three tables named: audit_logs, users, admin_users, whose corresponding ActiveRecord classes are: AuditLog, User, AdminUser.

Table users's id is integer type, and table admin_users's id is string type, AND BOTH admin_users and users has username field

The table audit_logs has :user_id(string) and :user_type two columns, and AuditLog has a belongs_to relationship: belongs_to :user, polymorphic: true

AND when I use ransack to search: AuditLog.ransack("user_of_User_type_username_eq => 'test')

I got the following error: ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: integer = character varying, it occurred when LEFT OUTER JOIN "users" ON "users"."id" = "audit_log.id".

I don't know if it is a correct behavior to raise error OR it is more reasonable to do a type cast in background in this case.

Thank you!

scarroll32 commented 4 years ago

@zhangyonguu is it possible to submit a PR with a failing test?