ernie/ransack — Issue #30: Search terms with '.' in polymorphic (STI) associations results in ActiveRecord::EagerLoadPolymorphicError
Author: ylansegal
Created: 2011-10-12 20:21:40+00:00
State: open

Ernie,

(Sorry for the long post, but I want to make sure you have all the information you need).

Given the following:

``` ruby
#person.rb
class Person < ActiveRecord::Base
end

#customer.rb
class Customer < Person
has_many :orders, :as => :customer
# I know it looks like I don't need the polymorphic association, but I have other classes that
# can also be customers, like InstitutionalCustomer, etc.
end

# order.rb
class Order < ActiveRecord::Base
belongs_to :customer, :polymorphic => true
end
```

When I try to search customer's emails I use (it's haml, but pretty simple markup):
```
= search_form_for [:admin, @q], :remote => true do |form|
%p
= form.label :first_name_or_last_name_cont, "Name cont"
= form.text_field :first_name_or_last_name_cont
%p
= form.label :email_cont
= form.text_field :email_cont
%p
= form.label :primary_phone_cont, "Phone"
= form.text_field :primary_phone_or_shipping_addresses_phone_or_billing_addresses_phone_cont
%p
= form.submit 'Search', :class => 'primary button'
```

That works just fine. However, when I try to search for orders by customer's emails:

```
= search_form_for [:admin, @q], :remote => false do |f|
%p
= f.label :customer_of_Person_type_first_name, 'First Name'
= f.text_field :customer_of_Person_type_first_name_cont
%p
= f.label :customer_of_Person_type_last_name, 'Last Name'
= f.text_field :customer_of_Person_type_last_name_cont
%p
= f.label :customer_of_Person_type_email_cont, 'Email'
= f.text_field :customer_of_Person_type_email_cont
%p
= f.submit 'Search', :class => 'primary button'
```

It results in an ActiveRecord::EagerLoadPolymorphicError (Can not eagerly load the polymorphic association :customer)

This happens when I use a search term that includes a '.' character (for example dan@dolphins.com). However if I use a term without the '.' then we do just fine (for example dan@dolphins).

Note that this only happens when searching for orders through the customer polymorphic association. If searching for customers only, the terms with '.' work just fine.

Thank you for your help and the gem (I am really making a whole lot of use both from ransack and squeel. Even using valium a bit now! Thanks again).