ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Searching for multiple attributes in belongs_to raises undefined method error

Open D4uS1 opened this issue 1 year ago • 1 comments

Hey all,

if i use a search field targeting multiple fields in a belongs_to association (like described in the documentation) i will get an "undefined method" error for "Ransack::Search<class: Customer, base: Grouping <combinator: and>>:Ransack::Search".

Suppose you have the following models:

class Customer < ApplicationRecord
  belongs_to :address, optional: true
end

class Address < ApplicationRecord
  has_many :customers, dependent: :nullify

  # this model has the attributes street_and_number, city, zip_code and country
end

Now if i submit the following search form:

<%= search_form_for @q do |f| %>
  <%= f.label 'Address' %>
  <%= f.search_field :address_city_or_address_zip_code_or_address_country_cont %>

  <%= f.submit "search" %>
<% end %>

I will get the following error:

undefined method `address_city_or_address_zip_code_or_address_country_cont' for Ransack::Search<class: Customer, base: Grouping <combinator: and>>:Ransack::Search

This seems to be only an issue for belongs_to associations. For has_many associations, this works fine. (For sure by using "addresses" instead of "address").

Thank you for help :)

D4uS1 avatar Jun 23 '23 13:06 D4uS1

I'm having the same problem here. I can provide a working example if that helps.

ronualdo avatar May 31 '24 14:05 ronualdo