Home>

ransackI would like to create a search form using and realize a multi-word search separated by spaces.
You can do "Search multiple words from a single column" or "Search a single word from multiple columns", but you cannot "Search multiple words from multiple columns". I tried to refer to the answer of the person who was asked in the past, but the SQL before separating by space separation is reissued and overwritten.if statementI tried to make a branch with, but it doesn't work. Can anyone please teach me?

before_action: set_search
  def set_search
    @categories = Category.where (ancestry: nil)
    if params [: q] .present?
      if params [: q] [: title_or_body_or_material_cont_any]! = nil
        texts = params [: q] [: title_or_body_or_material_cont_any] .split (/ [\ p {blank} \ s] + /)
        if texts.size>= 2 # If i do not branch whether it is multiple words, an error will occur in the case of a single word
          text_queries = texts.map do | text |
            Recipe.ransack (title_or_body_or_material_cont_any: text) .result
          end
          text_queries = text_queries.inject {| scope, query | scope&query}
          @search = text_queries.inject {| scope, query | scope.merge (query)} # (title or body or material) and (title or body or material)
          @search_recipes = @search # If i add ".page (params [: page]). Per (8)", an error will occur.
          @text_queries = text_queries # For checking the contents
        end
      end
      @search = Recipe.ransack (params [: q]) # Probably SQL overwrite occurs here?
      @search_recipes = @ search.result.page (params [: page]). per (8)
    else else
      params [: q] = {sorts:'created_at desc'}
      @search = Recipe.ransack ()
      @search_recipes = @ search.result.page (params [: page]). per (8)
    end
  end


It's a messy source because of trial and error, but I would appreciate it if you could also teach me how to write it concisely.

  • Answer # 1

    When this is confusing, I combine normal search with Ransack.

    First@q = Recipe.ransack (params [: q])However, this is only used to redisplay the search conditions on the result display screen, not for searching.

    title_or_body_or_material_cont_any = params [: q] .delete (: title_or_body_or_material_cont_any)
    And create a search condition using title_or_body_or_material_cont_any
    relation = Recipe. [Search condition using title_or_body_or_material_cont_any] @search_recipes = relation.result.page (params [: page]). per (8)
    Get in

    Postscript
    @categories = Category.where (ancestry: nil) if params [: q] .present? else elseChange between.

    @categories = Category.where (ancestry: nil)
    @q = Recipe.ransack (params [: q])
    if params [: q] .present?
      if t_b_m_m = params [: q] .delete (: title_or_body_or_material_cont_any)
        relation = Recipe.where ()
          #Here, using t_b_m_m, form relation = Recipe.where (). Where ()
          #Please assemble. Of course, you can use scope.
      else else
        relation = Recipe
      end
      @search = Rrelation.ransack (params [: q])
      @search_recipes = @ search.result.page (params [: page]). per (8)
    else else