Skip to content

Polymorphic filtering is inconsistent #1267

@stas

Description

@stas

Consider a model with two polymorphic relationships:

class Message < ApplicationRecord
  belongs_to :user
  belongs_to :from, polymorphic: true
  belongs_to :to, polymorphic: true

Now trying to filter based on any of the from_id OR to_id gives an error:

> Message.ransack(from_id_or_to_id_eq: '3d4464a4-1501-4f30-a892-fa07f72f9fa1').result.count
/usr/local/bundle/gems/activerecord-6.1.4.1/lib/active_record/reflection.rb:407:in `compute_class': Polymorphic associations do not support computing the class. (ArgumentError)

Which is strange as the query should not touch at all the polymorphic API since we query simply by foreign keys.
What's even more strange is that if I pass an arbitrary non-poly attribute to filter, everything works...

> Message.ransack(id_or_from_id_or_to_id_eq: '3d4464a4-1501-4f30-a892-fa07f72f9fa1').result.count
1

Would appreciate any help.
Thanks in advance 🙇

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions