Ruby on rails 我需要做一个高级搜索系统,但我不懂ruby,我有问题

Ruby on rails 我需要做一个高级搜索系统,但我不懂ruby,我有问题,ruby-on-rails,search,Ruby On Rails,Search,我正在做一个高级搜索系统,但是我没有结果,搜索根本不起作用,我是rails新手,从未应用过这个 我的型号 类产品{:medium=>“300x300>”,:thumb=>“100x100>”},:default\u url=>“default.jpg” 验证附件内容类型:图像、内容类型=>[“图像/jpg”、“图像/jpeg”、“图像/png”、“图像/gif”] 属于:类别 属于:用户 定义自搜索(参数) 条件_查询=[] 条件_值=[] 如果参数[:title]。是否存在? conditio

我正在做一个高级搜索系统,但是我没有结果,搜索根本不起作用,我是rails新手,从未应用过这个

我的型号

类产品{:medium=>“300x300>”,:thumb=>“100x100>”},:default\u url=>“default.jpg”
验证附件内容类型:图像、内容类型=>[“图像/jpg”、“图像/jpeg”、“图像/png”、“图像/gif”]
属于:类别
属于:用户
定义自搜索(参数)
条件_查询=[]
条件_值=[]
如果参数[:title]。是否存在?

conditions\u query看起来您的思路是正确的,但是搜索方法实际上并不执行任何查询,即使您构建了所有的条件。在最后一行稍作调整后,您可以得到以下结果:

def self.search(params)
  conditions_query = []
  conditions_value = []

  if params[:title].present?
    conditions_query << 'title LIKE ?'
    conditions_value << "%#{params[:title]}%"
  end

  if params[:category].present?
    conditions_query << 'category LIKE ?'
    conditions_value << "%#{params[:category]}%"
  end

  if params[:price].present?
    conditions_query << 'price LIKE ?'
    conditions_value << "%#{params[:price]}%"
  end
  where(conditions_query.join(" AND "), *conditions_value)
end

我想知道为什么这个问题被否决了。
class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_action :set_user, only: [:show, :edit, :update, :destroy, :create, :update, :new]



  # GET /products
  # GET /products.json
  def index
    @products = Product.all

    @user = User.all

  end

  # GET /products/1
  # GET /products/1.json
  def show
  end




  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
         @product.update(user_id: params[:user_id])
        format.html { redirect_to user_products_path(@user.id ,params[:product_id]), notice: 'Product was successfully created.' }
        format.json { render 'show', status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

def update
respond_to do |format|
if @product.update(product_params)
  format.html { redirect_to user_product_path, notice: 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end


# PATCH/PUT /products/1
# PATCH/PUT /products/1.json

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json




  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to user_products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:title, :price, :local, :description, :category_id, :contacts, :image, :user_id)
    end


    def set_user
      @user = User.find(params[:user_id])
    end


end
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  helper_method :current_user
  helper_method :cate
   def cate
    Category.all
   end

  private
   def current_user
    User.find_by id: session[:user_id] if session[:user_id] if session[:user_id]

   end
end
def self.search(params)
  conditions_query = []
  conditions_value = []

  if params[:title].present?
    conditions_query << 'title LIKE ?'
    conditions_value << "%#{params[:title]}%"
  end

  if params[:category].present?
    conditions_query << 'category LIKE ?'
    conditions_value << "%#{params[:category]}%"
  end

  if params[:price].present?
    conditions_query << 'price LIKE ?'
    conditions_value << "%#{params[:price]}%"
  end
  where(conditions_query.join(" AND "), *conditions_value)
end
scope :title_like, -> (title) { where('title LIKE ?', "%#{title}%") if title.present? }
scope :category_like, -> (category) { where('category LIKE ?', "%#{category}%") if category.present? }
scope :price_like, -> (price) { where('price LIKE ?', "%#{price}%") if price.present? }

def self.search(params)
  title_like(params[:title])
    .category_like(params[:category])
    .price_like(params[:price])
end