Created
November 19, 2020 11:04
-
-
Save progapandist/156ada869b997579edaf77652c29a5c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<%= form_tag movies_path, method: :get do %> | |
<%= text_field_tag :query, | |
params[:query], | |
class: "form-control", | |
placeholder: "Find a movie" | |
%> | |
<%= submit_tag "Search", class: "btn btn-primary" %> | |
<% end %> | |
<div class="row"> | |
<div class="col-sm-8 offset-sm-2"> | |
<div id="movies" class="mt-4"> | |
<% @movies.each do |movie| %> | |
<h4><%= movie.title %></h4> | |
<p><%= movie.syllabus %></p> | |
<% end %> | |
</div> | |
</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Movie < ApplicationRecord | |
belongs_to :director | |
include PgSearch::Model | |
multisearchable against: [:title, :syllabus] | |
scope :with_directors, ->(query){ | |
sql_query = " \ | |
movies.title @@ :query \ | |
OR movies.syllabus @@ :query \ | |
OR directors.first_name @@ :query \ | |
OR directors.last_name @@ :query \ | |
" | |
joins(:director).where(sql_query, query: "%#{query}%") | |
} | |
pg_search_scope :search_by_title_and_syllabus, | |
against: [ :title, :syllabus ], | |
using: { | |
tsearch: { prefix: true } # <-- now `superman batm` will return something! | |
} | |
pg_search_scope :global_search, | |
against: [ :title, :syllabus ], | |
associated_against: { | |
director: [ :first_name, :last_name ] | |
}, | |
using: { | |
tsearch: { prefix: true } | |
} | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MoviesController < ApplicationController | |
def index | |
if params[:query].present? | |
@movies = Movie.with_directors(params[:query]) | |
else | |
@movies = Movie.all | |
end | |
end | |
# def index | |
# if params[:query].present? | |
# sql_query = " \ | |
# movies.title @@ :query \ | |
# OR movies.syllabus @@ :query \ | |
# OR directors.first_name @@ :query \ | |
# OR directors.last_name @@ :query \ | |
# " | |
# @movies = Movie.joins(:director).where(sql_query, query: "%#{params[:query]}%") | |
# else | |
# @movies = Movie.all | |
# end | |
# end | |
# def index | |
# if params[:query].present? | |
# sql_query = " \ | |
# movies.title ILIKE :query \ | |
# OR movies.syllabus ILIKE :query \ | |
# OR directors.first_name ILIKE :query \ | |
# OR directors.last_name ILIKE :query \ | |
# " | |
# @movies = Movie.joins(:director).where(sql_query, query: "%#{params[:query]}%") | |
# else | |
# @movies = Movie.all | |
# end | |
# end | |
# def index | |
# if params[:query].present? | |
# sql_query = "title ILIKE :query OR syllabus ILIKE :query" | |
# @movies = Movie.where(sql_query, query: "%#{params[:query]}%") | |
# else | |
# @movies = Movie.all | |
# end | |
# end | |
# def index | |
# if params[:query].present? | |
# @movies = Movie.where("title ILIKE ?", "%#{params[:query]}%") | |
# else | |
# @movies = Movie.all | |
# end | |
# end | |
# def index | |
# if params[:query].present? # In a form, user typed "Batman" | |
# @movies = Movie.where(title: params[:query]) # params[:query] => "Batman" | |
# else | |
# @movies = Movie.all | |
# end | |
# end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TvShow < ApplicationRecord | |
include PgSearch::Model | |
multisearchable against: [:title, :syllabus] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment