Метод поиска Ruby - PullRequest
       5

Метод поиска Ruby

0 голосов
/ 15 декабря 2018

Я сделал этот метод для метода поиска в ruby ​​с формой.

Я думаю, что этот код можно упростить.Что ты думаешь ?

И у меня есть проблема.Когда пользователь ничего не вводит в форму, в результатах отображаются все подсказки в базе данных.

Но когда пользователь объединяет два поиска, например, ключевые слова и страну, в результате получаются все подсказки, в которых есть ключевые слова, а также все подсказки, касающиеся соответствующей страны.За исключением того, что я ищу для двух критериев, которые будут объединены.

Search.rb

require 'pry'

class Search < ApplicationRecord

  def self.search(keywords, category_id,city,country)
    table_of_ids_country = []
    table_of_ids_city = []
    table_of_ids_title = []
    table_of_ids_cats = []
    two_searches_ids = []
    merged_table_of_tips_ids = []
    @results = []

    tips_by_country = Tip.where(["country like?",country]) if country.present?

    tips_by_city = Tip.where(["city like?",city]) if city.present?

    tips_by_keyword = Tip.where(["title LIKE?","%#{keywords}%"]) if keywords.present?

    tips_by_cat = Category.where(["id = :id",{id:category_id}]) if category_id.present?

    two_searches = Tip.where(["title LIKE?",keywords]) if keywords.present? && Category.where(["id = :id",{id:category_id}]) if category_id.present?

    if tips_by_country != nil
      tips_by_country.each do |tip|
        tip.id
        table_of_ids_country << tip.id
      end
    end

    if tips_by_city != nil
      tips_by_city.each do |tip|
        tip.id
        table_of_ids_city << tip.id
      end
    end

    if tips_by_keyword != nil
      tips_by_keyword.each do |tip|
        tip.id
        table_of_ids_title << tip.id
      end
    end

    if two_searches != nil
      two_searches.each do |tip|
        tip.id
        two_searches_ids << tip.id
      end
    end

    if tips_by_cat != nil
      Category.find(tips_by_cat[0].id).tips.each do |tip|
        table_of_ids_cats << tip.id
      end
    end

    merged_table_of_tips_ids = [table_of_ids_title, table_of_ids_cats,table_of_ids_city,table_of_ids_country,two_searches_ids].flatten

    merged_table_of_uniq_tips_ids = merged_table_of_tips_ids.uniq


    merged_table_of_uniq_tips_ids.each do |tip|
       result = Tip.find(tip)
       @results << result
       binding.pry
    end
    return @results
  end
end

search_controller

require 'pry'
class SearchesController < ApplicationController

  def new
    @search = Search.new
  end

  def create
    @search = Search.create(search_params)
    redirect_to @search
  end

  def show
    @search = Search.find(params[:id])
    @search = Search.search(@search.keywords, @search.category_id,@search.city,@search.country)
    #Permet d'envoyer les paramètres au model search et à les réutilisé dans la méthode self.search
  end

private

  def search_params
    params.require(:search).permit(:keywords,:category_id,:id,:city,:country)
  end
end

моя форма:

<%=form_for @search do |f| %>
          <div class="form-group">
            <%= f.label :keywords, "Mots-clés" %>
            <%= f.text_field :keywords, class: "form-control", placeholder: "plongée, randonnée, temple..." %>
          </div>
          <div class="form-group">
            <%= f.label :city, "Mots-clés" %>
            <%= f.text_field :city, class: "form-control", placeholder: "plongée, randonnée, temple..." %>
          </div>
          <div class="form-group">
            <%= f.label :country, "Mots-clés" %>
            <%= f.text_field :country, class: "form-control", placeholder: "plongée, randonnée, temple..." %>
          </div>
          <div class="form-group">
            <%= f.label :category_id, "Catégories" %><br>
            <%= f.collection_select :category_id, Category.all, :id, :name, :include_blank => true %>
          </div>
          <div class="form-group d-flex justify-content-center">
            <%= f.submit "Rechercher", class: "btn btn-primary" %>
          </div>
          <%end%>

1 Ответ

0 голосов
/ 15 декабря 2018
class Search < ApplicationRecord

  def self.search(keywords, category_id,city,country)
    ids = if keywords.present? && category_id.present?
            Tip.keywords(keywords).pluck(:id) & Category.category_id(category_id).pluck(:id)
          elsif [keywords, category_id, city, country].any?(&:present?)
            Tip.country(country)
              .city(city)
              .keywords(keywords)
              .pluck(:id)
          else
            []
          end

    Tip.find(ids.uniq)
  end
end

class Category < ApplicationRecofd
  scope :category_id, ->(category_id) {
    where(["id = :id",{id:category_id}])
  }
end

class Tip < ApplicationRecord
  scope :country, ->(country) {
    where("country like?",country) if country.present?
  }

  scope :city, ->(city) {
    where("city like?",city) if city.present?
  }

  scope :keyword, ->(keywords) {
    tips = tips.where("title LIKE?","%#{keywords}%") if keywords.present?
  }
end

также обратите внимание, что я делаю объединение по идентификаторам категорий, так что имейте в виду

2.5.1 :002 > [1,2] && [2,3]
 => [2, 3] 
2.5.1 :003 > [1,2] & [2,3]
 => [2] 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...