Multi find search in ruby with date - ruby-on-rails

I have created a multi find search, where I need to filter records by date / category / title. Searching by a category and/or title works, however, when date is typed it doesn't change anything (the results is the same like there was no date typed). I have no idea what else I could do to fix it, I am just a beginner in Ruby. Any idea?
Model:
def self.multi_find(cat_id, search, date_search)
search_condition = "%" + search + "%"
#date_condition = date_search
# test if cat_id is not blank
if not cat_id.blank?
# assign the value of cat_id to a ‘scope’ named :cat
scope :cat, -> { where('category_id = ?', cat_id) }
# using the ‘scope’ cat find where a search string is like a title or an author’s name
self.cat.where("title LIKE ? or event_date = ?", search_condition, date_search.to_date)
else
# find where a search string is like a title or an author’s name
self.where("title LIKE ? or event_date = ?", search_condition, date_search.to_date)
end
end
Controller:
def multi_find
# call an Event class method, using two parameters; a category unique identifier and a search string (author or title)
events = Event.multi_find(params[:cat_id], params[:search_string], params[:event_date_search])
# use Kaminari pagination ...
#events = Kaminari.paginate_array(events.order :title).page(params[:page]).per(6)
# if no products have been found
if #events.empty?
# display a notice
flash.now[:alert] = "No events found - so displaying all events"
# then display all products
#events = Event.order(:title).page(params[:page]).per(6)
end
# use the index view
render :action => "index"
end
The console outputs the SQL Query
Event Load (0.0ms) SELECT "events".* FROM "events" WHERE (category_id = '1') AND (title LIKE '%%' or event_date = '2018-02-14') ORDER BY "events"."title" ASC
View file:
<%= form_tag my_path, :method=>'post', :multipart => true do %>
<%= select_tag ('cat_id'),
options_from_collection_for_select(#categories, :id, :cat_name, 0 ),
:prompt => "Select a Category" %>
<div class="datepicker">
<% #event_date_format %>
<%= text_field_tag :event_date_search %>
</div>
<!-- Key word:-->
<%= text_field_tag :search_string %>
<%= submit_tag 'Search' %>
<% end %>

It's because you have an or in your sql statement. However you should also clean up your code a bit.
def self.multi_find(cat_id, search, date_search)
result = self.all
result = result.where(category_id: cat_id) if cat.id.present?
result = result.where('title LIKE ?', "%#{search}%") if search.present?
result = result.where(event_date: date_search) if date_search.present?
result
end

Related

Rails 5 Checkbox filter

I can't seem to find a simple solution to this anywhere. Simply put, I just want to be able to filter items with checkboxes and have the option to select multiple boxes and the filter work. The parameters get inputted, but the results don't appear (see below).
If I remove the [] from view, I can get single items to search but not multiples. The [] encodes %5B%5D into the URL which I think is part of reason search won't work. Please anyone who can help.
Controller:
#cars = if params[:colour_category]
Car.where('colour_category LIKE ? OR design LIKE ?', "%#{params[:colour_category]}%", "%#{params[:design_category]}%")
else
Car.all
end
View:
<%= form_tag cars_path, method: :get do %>
<%= check_box_tag('colour_category[]', "Yellow") %>
<%= check_box_tag('colour_category[]', "Blue") %>
<%= check_box_tag('design_category[]', "Luxury") %>
<%= submit_tag "Submit" %>
<% end %>
Example of parameters in server after doing a submit: Parameters: {"utf8"=>"✓", "colour_category"=>["Yellow", "Blue"], "commit"=>"Submit"}
A cleaner approach, which chains different scope/filter methods based on the params values. I'm posting an example from one of my apps, you can adapt the same concept:
module Client::Filterable
extend ActiveSupport::Concern
include PgSearch::Model
included do
scope :find_by_company, -> (company_id) { where(company_id: company_id) }
pg_search_scope :search, against: %w( name ), using: {
tsearch: { prefix: true }
}
end
class_methods do
def filter(params = {})
relation = self
relation = relation.search(params[:query]) if params.fetch(:query, false).present?
relation = relation.find_by_company(params[:company_id]) if params.fetch(:company_id, false).present?
relation.all
end
end
end
then in your controller:
#clients = Client.all.filter(query: 'John Smith', company_id: 4356)
You can add as many filter options into the #filter method, they'll only be applied if the relevant param key is present.
def filter(params = {})
relation = self
relation = relation.by_color_category(params[:color_category]) if param.fetch(:color_category, false).present?
relation = relation.by_design_category(params[:design_category]) if param.fetch(:design_category, false).present?
relation.all
end

Search/ filter method for all attributes in my index table

I'm trying to write a row for my index table that filters my objects regarding a specific value of a specific column. What I have until now is this:
pimps_controller.rb:
def index
#pimps = Pimp.search(params[:search])
end
pimp.rb:
def self.search( search)
if search
where('title LIKE ?', "%#{search}%")
else
scoped
end
end
A part of view:
<%= text_field_tag :search, params[:search] %>
That filters after the objects title only so I tried to alter it to make it functional for different search fields that can filter after different attributes. I want to pass a second parameter value if someone fires the search function to make sure it triggers for the right attributes. That's what I've tried:
pimps_controller.rb
#pimps = Pimp.search(params[:search_column],params[:search])
pimp.rb:
def self.search(search_column, search)
if search
col = "%#{search_column}"
s = "%#{search}%"
where(col 'LIKE ?', s)
else
scoped
end
end
The view:
<%= text_field_tag :search, params[:search], params[:search_column => title] %>
But it's not working. I get an error message for passing the both parameters in one search field I guess. How would you do it?
Here's a simple tutorial on how to do it:
https://we.riseup.net/rails/simple-search-tutorial
In the model, you will have to add the fields with or condition to the query.
def self.search(search)
search_condition = "%" + search + "%"
find(:all, :conditions => ['title LIKE ? OR description LIKE ?', search_condition, search_condition])
end
If you want to define the field to search in the params you can use string interpolation with simple quotes:
%q(text contains "#{search.query}")
You need 2 text fields, one for the column, one for the value:
# view
<%= text_field_tag :search_value, params[:search_value] %>
<%= text_field_tag :search_column, params[:search_column] %>
# controller
#pimps = Pimp.search(params[:search_column], params[:search_value])
# Pimp model
def self.search(search_column, search_value)
if search_value.present? && search_column.present?
column = self.column_names.include?(search_column.to_s) ? search_column : 'title'
value = "%#{search_value}%"
where("#{self.table_name}.#{column} LIKE ?", value)
else
scoped
end
end
The problem about this method is that if you don't type the exact name of the column, it will search the value in the column title. I think you should use a select_tag, listing all searchable columns of the model:
# view
<%= select_tag :search_column, options_for_select(Pimp.column_names.map { |col| [col, col] }, params[:search_column]) %>
This view code will display a select tag with the available columns of the Pimp model. You can easily limit the searchable columns by defining a class method on Pimp:
# Pimp model
def searchable_columns
self.column_names - ['id', 'created_at', 'updated_at']
end
# view
<%= select_tag :search_column, options_for_select(Pimp.searchable_columns.map { |col| [col, col] }, params[:search_column]) %>

undefined method 'map' for String

I have a Rails 4 application. I have a search function with a select field. Now I'm getting this error:
NoMethodeError: undefined method 'map' for "5":String: select * from users inner join user_texts on users.id = user_id where text_id = ?
Here's view:
<% form_tag user_path do
<% select_tag :text_id, Option_from_collection_for_select(text.all, :id, :name, params[:text_id]), include_blank: true %>
<%= button_to '#', class: 'btn btn-default' do %>
<% t(:find) %>
<% end %>
Here's controller:
def index
if params[:text_id].present?
#users = User.search params[:text_id]
end
#users = User.all
end
Here's model:
def self.search(text_id)
find_by_sql("select * from users inner join user_texts on users.id = user_id where text_id = ?
", text_id)
end
When I do this, it works perfect:
select * from users inner join user_texts on users.id = user_id where text_id = 5
Change your method to this
def self.search(text_id)
User.joins(:user_texts).where("text_id = ?", text_id)
end
Two reasons why you should change
This is more rails-y way of doing it
find_by_sql doesn't work well with query placeholders
The above answer is perfectly fine and recommended, but sometimes you have to fetch from a raw sql query. And the error above is we need to use square brackets [ instead of round ( ones. Check out the API
So this should have worked.
def self.search(text_id)
find_by_sql ["select * from users inner join user_texts on users.id = user_id where text_id = ?
", text_id ]
end

Ransack search not working if there is 'space' in search term

I am using ransack for search in my rails 3.2 application using postgres as database.
I have a Invoice model and every invoice belongs_to a buyer. Below is my search form in index page.
views/invoices/index.html.erb
<%= search_form_for #search do |f| %>
<%= f.text_field :buyer_name_cont %>
<%= f.submit "Search"%>
<% end %>
And here is my controller code.
controllers/invoices_controller.rb
def index
#search = Invoice.search(params[:q])
#invoices=#search.result(:distinct => true).paginate(:page => params[:page], :per_page => GlobalConstants::PER_PAGE )
respond_to do |format|
format.html # index.html.erb
format.json { render json: #invoices }
end
end
Let's say a invoice is there of a buyer having name "Bat Man".
If I search "Bat", I get the invoice in results.
Again if I search "Man", I get the invoice in results.
But if I search "Bat Man", I don't get the invoice in results.
I know it might be something trivial but I am not able to resolve.
Update
When I tried the sql query formed directly in database using pgAdmin, I realized that in database there were multiple spaces in the buyer name, something like "Bat.space.space.space.Man".
Can something be done so that "Bat.space.Man" search also finds "Bat.space.space.space.Man" in results?
You could sanitize your data. For instance with regexp_replace(). Run in the database once:
UPDATE invoice
SET buyer = regexp_replace(buyer, '\s\s+', ' ', 'g')
WHERE buyer <> regexp_replace(buyer, '\s\s+', ' ', 'g');
And sanitize new inserts & updates likewise.
\s .. class shorthand for "white space" (including tab or weird spaces).
The 4th parameter 'g' is for "globally", needed to replace all instances, not just the first.
Ransack not support cont search for multi terms, I solved the requirement my customized way. the details as following:
Add scope to your model:
scope :like_search, ->(column, value) {
keywords = value.to_s.split.map{ |k| "%#{k}%" }
where(Array.new(keywords.size, "#{column} ILIKE ?").join(' AND '), *keywords)
}
in your view. instead of using f.text_field :buyer_name_cont provided by ransack, use normal field helper text_field_tag :buyer_name, params[:buyer_name]
then restrict your ransack in scope:
scope = Invoice.like_search(:name , params[:buyer_name])
#q = scope.ransack(params[:q])

Next and Previous records

In my show.html.erb file I have two links for next and previous like so
<%= link_to("Previous Post", #question.previous) if #question.previous %>
<%= link_to("Next Post", #question.next) if #question.next %>
.previous and .next are methods in my model which finds either the current id + 1 or - 1
like so in my model
def previous
Question.find_by_id(id - 1, :select => 'id')
end
def next
Question.find_by_id(id + 1, :select => 'id')
end
But rather than incrementing and decrementing blindly as I might have deleted a "Question" so i'll get an error, could I find the next/previous "Question" from the scope of the query the show action was accessed from?
Have you tried something like this?
def previous
Question.limit(1).order("id DESC").where("id < ?", id)
end
def next
Question.limit(1).order("id DESC").where("id > ?", id)
end
Try Act as Ordered.
https://github.com/imedo/acts_as_ordered

Resources