I'm trying to figure out how to use the LIKE statement to achieve the following in Postgres:
Data: Test Project
The following search queries should return Test Project:
test est est project pro
Currently my method in the my projects.rb model looks like this:
self.where('name LIKE ?', "%#{search}%")
(I'll be adding downcase of course) It currently fetches any variation of the first word of the :name attribute value but no the second, so searching pro would return nothing.
projects.rb model
def self.search(search)
if search
scope :find_name, lambda { |search| where("name ILIKE :search", search: "%#{search.downcase}%") }
else
self.where(nil)
end
end
projects_controller.rb
def index
@projects = Project.search(params[:search]
end
index.html.erb
<p id="notice"><%= notice %></p>
<div class="projects-index">
<div class="container-body text-center">
<h1>Projects</h1>
<%= form_tag projects_path, method: :get, id: "projects_search" do %>
<p>
<%= text_field_tag :search %>
</p>
<% end %>
</div>
<!-- CARDS -->
<div class="row cards-section">
<div id="projects">
<%= render partial: 'projects' %>
</div>
<div class="col-3">
<%= link_to new_project_path, class: "new-project text-center" do %>
<div class="align vertical">
<div class="align-flex center">
<%= image_tag "add_project.png" %>
</div>
<p>New Project</p>
</div>
<% end %>
</div>
</div>
</div>
_projects.html.erb partial
<% @projects.each do |project| %>
<div class="col-3">
<div class="existing-card">
<div class="top">
<div class="ui icon left pointing dropdown button">
<i class="ellipsis vertical icon"></i>
<div class="menu">
<div class="item">
<%= link_to 'Edit', edit_project_path(project), class: "link" %>
</div>
<div class="item">
<%= link_to 'Share', new_user_invitation_path, class: "link" %>
</div>
<div class="item">
<%= link_to 'Delete', project_path(project), method: :delete, data: { confirm: 'Are you sure you want to delete this project?' }, class: "link" %>
</div>
</div>
</div>
</div>
<div class="card-container align-flex center">
<p><%= project.name %></p>
</div>
</div>
</div>
<% end %>