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

Search by two fields with the same name (one for model, one for association) #70

Open
mklimkin0190 opened this issue Sep 6, 2018 · 1 comment

Comments

@mklimkin0190
Copy link

Hi there!

Sorry if similar issue already exists, but I couldn't find anything on the matter.

I have a model (let's say Product) that belongs_to another model - Category. I need to implement search on products by two fields: product.name and category.name (using assoc: ['category']). Both fields have the same name - name.

This code (surprisingly) works:

<%= search_form(@conn, @rummage, [fields:
  [
    name: %{label: "Search by category name", search_type: "ilike", assoc: ["category"]},
    name: %{label: "Search by name", search_type: "ilike"}
  ], button_class: "btn",
]) %>

But if I change the order of fields, it only filters by the association name.

<%= search_form(@conn, @rummage, [fields:
  [
    name: %{label: "Search by name", search_type: "ilike"},
    name: %{label: "Search by category name", search_type: "ilike", assoc: ["category"]}
  ], button_class: "btn",
]) %>

If I skip the search_form and implement the same form using form_for and inputs_for, this inconsistency persists.

I realize that this case should probably be handled differently, but I can't find any documentation or example of the correct way of doing it, so any help is appreciated.

@thebugcatcher
Copy link
Contributor

Hey @0190, This is a genuine bug, with frankly only one type of solution, which is creating two separate forms. rummage_ecto 2.0 is coming with features like rummage_scope which will make defining these fields easier. But even with that, you will need two forms. Unfortunately, I don't see a cleaner way of doing this.

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

2 participants