ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Polymorphic filtering is inconsistent

Open stas opened this issue 4 years ago • 0 comments

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 :bow:

stas avatar Dec 16 '21 19:12 stas