Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching for multiple attributes in belongs_to raises undefined method error #1428

Open
D4uS1 opened this issue Jun 23, 2023 · 0 comments
Open

Comments

@D4uS1
Copy link

D4uS1 commented Jun 23, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant