Ruby on rails. Ransack search associations - ruby-on-rails

I'm getting a little stuck with a ransack search form.
I need to search through a model's association, and bring results back when name_cont BUT ONLY WHEN AN associated model's status == something.
I have a gig model with a has_many belongs_to relationship with a request model and user model. The request model has attributes status and the user model has attributes publicname. I want to search publicname_cont and only show results from the requests that have a status == "hired".
Currently my search brings back the results of publicname regardless or the request models status.
Gigs Controller:
#gigs = #q.result(distinct: false).notexpired.order(date: :asc).page(params[:page]).per(20)
Search form:
<%= search_form_for #q, url: welcome_index_url, remote: false do |f| %>
<%= text_field_tag :search, params[:search], class: 'loc-search', placeholder: "Location"%>
<%= f.search_field :locationname_cont, placeholder: "Bar name" %>
<%= f.search_field :requests_user_publicname_cont, placeholder: "Band name" %>
<%= f.submit %>
<% end %>
Currently, I can search requests_user_publicname_cont and it gives me all the results as expected where the parameters match.
I am looking for a way to search requests_user_publicname_cont AND requests_status_eq, ("hired")
I am not sure how to go about this, any help would be appreciated, thanks.

#q = Gig.joins(:requests).where(requests: { status: 'hired' }).ransack(params[:q])

Related

How do I add a filter to the Ransack search function in Rails?

I have implemented Ransack's search function, and now I need to add a filter to let the list filter according to the "status" of the drop-down menu -- that is, prompt: t('all_status') in the code below, and only display records matching this state.
I have followed the instructions in Ransack's "extensive documentation" (Sorting in the View section) to try it out, but the result kept throwing Syntax errors, and finally only the "Filter" button with no function was left. I don't want to give up Ransack's search function, please tell me how to add filter.
this is the View code
<%= search_form_for(#query, class: 'form' , url: list_tasks_path) do |f| %>
<%= f.label :name, t('search') %>
<%= f.search_field :name_or_note_cont, placeholder: t('task_name_note') %>
<%= f.select :status_eq, Task.statuses.map { |key, value| [t("checkpoint_status.#{key}"), value] }, prompt: t('all_status') %>
<%= f.submit %>
<% end %>
this is the relevant controller#action
def list
#query = current_user.tasks.ransack(params[:q])
#tasks = #query.result.page(params[:page]).per(10)
end

How to Pass Form Field Values to a Different Controller View in Rails?

In my Rails 6 app, I have a Product and Order model.
On my products#show page, I have some fields and a button. What I need to do is send the info to the orders#new page so that this data is shown on the orders#new page.
I have tried to write some code based on Pass variables without model associations in Rails and how to pass parameters in params using form_tag method in rails, but my code seems completely wrong.
on products#show:
<%= form_tag(new_order_path do |form| %>
<%= form.input_field :comments %>
<%= form.button %>
<% end %>
With this code I get undefined method text_field' for nil:NilClass`.
I have tried adding attr_accessor :comments to both the Product and Order model, but it doesn't help.
I don't think that my approach or what I am trying to code is right. I was just trying to piece together parts from these answers.
Can someone please help me figure out the best way I can pass this data to Orders#new to show in that view?
<%= form_with url: "/search", method: :get do |form| %>
<%= form.label :query, "Search for:" %>
<%= form.text_field :query %>
<%= form.submit "Search" %>
<% end %>
from the docs
https://guides.rubyonrails.org/form_helpers.html
and the show path also requires an id

Fetch Data From Multiple Models In One View

This seems easy but I can't figure out how to do it
I'm trying to implement Ransack search in my rails app and for that I have a generated a model Cofounder which don't have any fields in it and I have association between Cofounder and CustomUser model which has email and other fields but i only want to search through email using ransack, i have this association between these two:
has_many :cofounders
belongs_to :CustomUser
(Do i need to have id if Yes how do i do it)
CoFounder Model
class Cofounder < ActiveRecord::Base
belongs_to :CustomUser
end
CofoundersController
class CofoundersController < ApplicationController
layout "custom_layout", only: [:index]
def index
#q = Cofounder.ransack(params[:q])
#cofounders = #q.result
#cofoundes = CustomUser.all
#countries = Country.all
end
end
and in my index view
<% search_form_for #q, url: cofounders_index_path do |f| %>
<%= f.search_field :email_cont, placeholder: "Search Here" %>
<%= f.submit "Search" %>
<% end %>
<%= select_tag "search_by_country", options_for_select(#countries.collect{|x| [x.name]} , params[:search_by_country] ),{prompt: "Select country"} %><br><br>
<%= select_tag "choose_a_role", options_for_select(#cofoundes.collect{|x| [x.first_name]} , params[:choose_a_role] ),{prompt: "Choose A Role", id: "select", multiple: true} %><br><br>
<% #cofoundes.each do |cofounder| %>
<%= cofounder.email %>
<%end%>
it is giving me error code
undefined method `email_cont' for #
i know email_cont if not a field in cofounder and i'm accessing that field in cofounder which is giving me error, how do i get those values in cofounder for successful Ransack search.
any help would be appreciated :)

Ransack search by a selected column dynamicaly

i want to do a search with Ransack in a User model, but i need a select with the attributes of the model as options to perform the search for that model. So if i have as attributes name, last_name, email the select should have this fields to be selected and next to it an input field to write the text that i want to search in the selected column or attribute for the User model.
Is there a way to do it with Ransack in Rails 4.1?
I think that could be something like this
<%= search_form_for #q do |f| %>
<%= f.select :selected_field, collection_of_attributes %>
<%= f.search_field :user_selected_field_cont %>
<%= f.submit %>
<% end %>
Any help will be appreciated :)
Try using ransack's attribute_select. http://railscasts.com/episodes/370-ransack?view=asciicast

Search form using sunspot/solr

I'm using sunspot for the first time and i'm trying to setup the search. full text search seems to work fine. however, i have a form with a search box and multiple filters on boolean fields that the user can select. somehow the search box works fine but solr isn't picking up the individual booleans as additional filters. also, when i don't do any search text and just want to search by the boolean fields, nothing happens. any help would be appreciated:
this is my controller:
#search = Project.search do
fulltext params[:search]
facet(:master_bedroom)
facet(:dining_room)
facet(:bath)
with(:master_bedroom, params[:mb]) if params[:mb].present?
with(:dining_room, params[:dr]) if params[:dr].present?
with(:bath, params[:p_bath]) if params[:p_bath].present?
end
i have the fields in the model:
searchable do
text :description
boolean :dining_room
boolean :bath
boolean :master_bedroom
end
and i have the following for my view:
<%= form_tag projects_path, :method => :get do %>
<%= text_field_tag :search, params[:search] %>
<%= check_box_tag :bath, 'true'%>
<%= submit_tag "Search", :name => nil %>
<% end %>
There was an error in variable naming.

Resources