Multiple Text Search issue - ruby-on-rails

I created a search application using 2 columns (name and lastname) but is not working when a person has more than 2 names. How can i search a person with 3 names and lastname?
Controller:
class PersonController < ApplicationController
def search
#people = Person.find(:all,:conditions=>["(
name LIKE ? OR
lastname LIKE ? OR
(concat(name, \" \", lastname) LIKE ? )
)" ,
"%#{params[:query]}%",
"%#{params[:query]}%",
"%#{params[:query]}%" ]
)
end
end
View
<% form_tag :controller=>"person",:action=>"search" do %>
Nombre o Apellido
<%= text_field_tag "query", params[:query]
<% end %>
Example
|name| |Lastname|
Marcos Ignacio Raul Perez
I saw on my logs this:
Works if i search Marcos Ignacio Raul Perez
SELECT * FROM `people`
WHERE ((
name LIKE '%Marcos Ignacio Raul Perez%' OR
lastname LIKE '%Marcos Ignacio Raul Perez%'
OR (concat(name, " ", lastname) LIKE '%Marcos Ignacio Raul Perez%' )
))
Works if i search Ignacio Raul Perez
SELECT * FROM `people`
WHERE ((
name LIKE '%Ignacio Raul Perez%' OR
lastname LIKE '%Ignacio Raul Perez%'
OR (concat(name, " ", lastname) LIKE 'Ignacio Raul Perez%' )
))
Works if i search Raul Perez
SELECT * FROM `people`
WHERE ((
name LIKE '%Raul Perez%' OR
lastname LIKE '%Raul Perez%'
OR (concat(name, " ", lastname) LIKE 'Perez%' )
))
Doesn't work if i search Marcos Perez
SELECT * FROM `people`
WHERE ((
name LIKE '%Marcos Perez%' OR
lastname LIKE '%Marcos Perez%'
OR (concat(name, " ", lastname) LIKE '%Marcos Perez%' )
))
Doesn't work if i search Ignacio Perez
SELECT * FROM `people`
WHERE ((
name LIKE '%Ignacio Perez%' OR
lastname LIKE '%Marcos Perez%'
OR (concat(name, " ", lastname) LIKE '%Ignacio Perez%' )
))
Doesn't work if i search Marcos Raul Perez
SELECT * FROM `people`
WHERE ((
name LIKE '%Marcos Raul Perez%' OR
lastname LIKE '%Marcos Raul Perez%'
OR (concat(name, " ", lastname) LIKE '%Marcos Raul Perez%' )
))
I will really appreciate help.

I highly recommend using elasticsearch with tire gem or something similar instead of SQL. It looks like your search requirements are complex. Problem that you are having with search in SQL is a sign of missing capabilities in underlying application architecture.

You can use Thinking sphinx is a good gem for rails search

Related

select2 How to search by value?

How to search by value? I tried many methods but it didn't work.
<option value="276" data-select2-id="475">Beaconsfield Village House</option>
or Also i changed templateSelection and templateResult to this template
<li class="select2-selection__choice" title="Beaconsfield Village House" data-select2-id="478">Beaconsfield Village House<span class="cat-id"> ID 276</span></li>
But still when i type 276 it says no results found.
escapeMarkup:function(e){return e},templateResult:function(e){return e.text + '<span class="cat-id"> ID ' + e.id + '</span>'},templateSelection:function(e){return e.text + '<span class="cat-id"> ID ' + e.id + '</span>'},theme:"default",width:"300"
How to solve this problem? I want to search with value. Thanks in advance.
Select2 v4.0.13

Getting {", value= "=>"on"} as a paramater when I want an Object's id

I have a program that is supposed to simulate a delivery POS. A Delivery Object belongs_to a Meal Object, which has_many items. The form renders a bunch of items with a checkbox next to each item using helper methods like this...
#inside the app/models/item.rb file
def self.meal_field_maker(foods)
rStr = ""
foods.each do | sel |
rStr += "<p><input type= 'checkbox' name= 'meal[items][], value= '#{sel.id}'> #{sel.show} </p>"
end
return rStr
end
#inside the app/helpers/application_helper.rb file
def the_new_meal_form
foodgroups = Item.get_foodgroups #Gets each food group
rStr = ""
foodgroups.each do | sel |
rStr+= "\n<h3>#{sel}</h3>" #Adds the name of each Food Group as a header before checkboxes
groupedFoods = Item.list_items_of_group(sel) #Gets every item of a food group
rStr += Item.meal_field_maker(groupedFoods) #makes a checkbox input tag for each item in groupedFoods
end
return (rStr)
end
And this all renders properly in the form which looks like this...
<form method= "post" action= "new_user_delivery">
<input type= "hidden" name= "delivery[user]" value= <%= #user.id %>
<%= user_delivery_address_field(session).html_safe %>
<p>(Optional) Meal Name: <input type= "text" name="delivery[meal][name]"></p>
<p>----------------------------------------------------</p>
<%= the_new_meal_form.html_safe %>
<p>----------------------------------------------------</p>
<p>Proceed to Payment <input type= "submit" value= "Proceed">
</form>
And It looks like it all works perfectly, but on the next action the params look like this...
<ActionController::Parameters {"delivery"=>{"user"=>"11", "address"=>"98 Linden Ave", "meal"=>{"name"=>"FirstMeal"}}, "meal"=>{"items"=>[{", value= "=>"on"}, {", value= "=>"on"}, {", value= "=>"on"}, {", value= "=>"on"}, {", value= "=>"on"}]}, "controller"=>"deliveries", "action"=>"payment_options", "id"=>"11"} permitted: false>
The issue here is clearly that for every item I select, I just get {", value= "=>"on"}, which of course gives me no indication which Item Objects were chose, as you see this is ~supposed~ to return the Item's ID as the parameter value. How do I fix this?
Your line here:
rStr += "<p><input type= 'checkbox' name= 'meal[items][], value= '#{sel.id}'> #{sel.show} </p>"
is missing a single quote after 'meal[items][]. Also make sure you don't have spaces immediately after equals signs:
rStr += "<p><input type='checkbox' name='meal[items][]', value='#{sel.id}'> #{sel.show} </p>"
I would say that it's generally a code smell if you have HTML building code inside your model. It's usually better to keep those in views or helpers, but that's outside the scope of this question.

How to parse an HTML webpage and remove <br> tags?

i need to parse a website that contains <p> tags (many of them) i want to get them and put them on a csv file (in same column).
After testing, i'm seeing the paragraphs are not on the same column, it's because of the <br> that are on <p> tags example :
HTML :
<div class="text">
<p> hello <br> friends </p>
<p> parsing is cool <br> using <br> simpleHTMLdom </p>
</div>
When i parse the html below i get the two <p> but not on same csv "column".
My code :
if($html_book_page->find('.text')){
foreach($html_book_page->find('div[class=text] p') as $bookPreview){
array_push($book, $bookPreview->plaintext);
}
}
$book is the array containing all text and i put $book on csv like :
fputcsv($open_csv, array_values($book), ',', ' ');
Any way to get :
(header of csv : TEXT ) and inside :
"Hello friends parsing is cool using simpleHTMLdom" ? Because for moment i have "Hello" and in another column i've "friends" .. "parsing is cool" ..."using".... "simpleHTMLdom"
Thank you all
Why don't you do a jQuery.remove() before your CSV insert? Something like this:
$('.text p').find('br').remove()
If you don't want to permanently remove <br> from the page, you could do something like this in your for-loop:
foreach($html_book_page - > find('div[class=text] p') as $bookPreview) {
$bookPreview.innerHTML.replace("<br>", "");
array_push($book, $bookPreview - > plaintext);
}

options_from_collection_for_select how to use two variables as display values

I have a select tag like so:
= select_tag :cc, options_from_collection_for_select(#possible_contacts, "id", "email")
this lists each of #possible_contacts email addresses. each contact also has a name. what I would like to do is display each #possible_contacts as a name and email address like so:
Joe Smith <joe#gmail.com>
this requires both email and name...but I'm not sure how to put both of them in the select
You could use options_for_select instead, something like:
select_tag :cc, options_for_select(#possible_contacts.map{|contact| ["#{contact.name} <#{contact.email}>", contact.id])
For example, this is basically the same as:
select_tag :cc, options_for_select([['Joe Smith <joe#example.com>', 123], ['David H Hansson <dhh#example.com>', 987]])
which turns into:
<select id="cc" name="cc">
<option value="123">Joe Smith <joe#example.com></option>
<option value="987">David H Hansson <dhh#example.com></option>
</select>

Grails select integer value and string value

I have two domain classes...
First:
class Employee {
String vorname
String nachname
Department department
}
Second:
class Department {
Integer number
String description
}
...and I want to create a dropdown in Grails.
My code looks like this:
<g:select id="department" name="department.description" from="${workloadreport.Department.list()}" optionValue="bezeichnung" optionKey="id" required=""
value="${employeeInstance.department?.id}" class="many-to-one" noSelection="${['null':'Please select...']}"/>
But I want to show the number + description as one value in dropdown.
Like:
Value 1 : 30 General Services
Value 2 : 40 Product Services
Value 3 : 50 Other Services
I'm assuming you want the resulting html to look something like this for an option element ...
<option value="1">1 : 30 General Services</option>
this should produce it (there may be typo, I haven't tested it):
<g:select id="department" name="department.description"
from="${workloadreport.Department.list()}" optionKey="id" required=""
value="${employeeInstance.department?.id}" class="many-to-one"
noSelection="${['null':'Please select...']}"
optionValue="${{it.id + ' : ' + it.number + ' ' + it.description}}"
/>
** note you probably have a typo in your from property. If you just want to generate a list of options from all departments, your from should look like this
<g:select ... from="${Department.list()}" .../>

Resources