Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mongoid查询-优化搜索实现_Ruby On Rails_Mongodb_Ruby On Rails 4_Mongoid_Mongoid3 - Fatal编程技术网

Ruby on rails Mongoid查询-优化搜索实现

Ruby on rails Mongoid查询-优化搜索实现,ruby-on-rails,mongodb,ruby-on-rails-4,mongoid,mongoid3,Ruby On Rails,Mongodb,Ruby On Rails 4,Mongoid,Mongoid3,我正在Rails4和Mongoid中进行优化搜索。这是我的搜索参数和表格 我逐个构建了查询: if params[:procedure_id] @blood_banks = BloodBank.where(:procedure_id => params[:procedure_id]) end if params[:location_ids] @blood_banks = BloodBank.where(:location_id.in => params[:location_

我正在Rails4和Mongoid中进行优化搜索。这是我的搜索参数和表格

我逐个构建了查询:

if params[:procedure_id]
  @blood_banks = BloodBank.where(:procedure_id => params[:procedure_id])
end
if params[:location_ids]
  @blood_banks = BloodBank.where(:location_id.in => params[:location_ids])
end
...
if condition
end
如果我需要在“Hebbal”位置搜索程序“椎间盘切除术”,那么我需要如下定义If条件

if params[:procedure_id].present? && params[:location_id].present?
...
end
然后我需要为所有这些搜索组合(4x3x2x1)做优化搜索!!! 这是实现相同功能的最佳方法

你是如何达到上述情况的

我不能对所有可能的条件都假设,是他们的任何捷径方法

如何实现以下代码:

if params[:fees].present?
      if params[:fees] == "300"
        @doctor_clinic = DoctorClinic.where( :consultation_fees => { '$lte' => params[:fees] }).map(&:doctor_id)
        @doctors = Doctor.where(
          { :id.in => @doctor_clinic })
      end
      if params[:fees] == "500"
        @doctor_clinic = DoctorClinic.where( :consultation_fees => { '$gte' => 300, '$lte' => params[:fees] }).map(&:doctor_id)
        @doctors = Doctor.where(
          { :id.in => @doctor_clinic })
      end
      if params[:fees] == "1000"
        @doctor_clinic = DoctorClinic.where( :consultation_fees => { '$gte' => 500, '$lte' => params[:fees] }).map(&:doctor_id)
        @doctors = Doctor.where(
          { :id.in => @doctor_clinic })
      end
      if params[:fees] == "1001"
        @doctor_clinic = DoctorClinic.where( :consultation_fees => { '$gte' => params[:fees] }).map(&:doctor_id)
        @doctors = Doctor.where(
          { :id.in => @doctor_clinic })         
      end
    end
您可以这样做:

conditions = {}
conditions.merge!(:procedure_id => params[:procedure_id]) if params[:procedure_id]
conditions.merge!(:location_id.in => params[:location_ids]) if params[:location_ids]
...
@blood_banks = BloodBank.where(conditions)
编辑

关于上一个代码,因为这里没有逻辑(所有值的共同概念),所以可以使用
case

condition = case params[:fees]
  when "300"
    { '$lte' => 300 }
  when "500"
    { '$gte' => 300, '$lte' => 500 }
  when "1000"
    { '$gte' => 500, '$lte' => 1000 }
  when "1001"
    { '$gte' => 1001 }
  else
    nil
end

if condition
  @doctor_clinic = DoctorClinic.where( :consultation_fees => condition).map(&:doctor_id)
  @doctors = Doctor.where({ :id.in => @doctor_clinic })
end

谢谢,这对我帮助很大。这就是我要找的。你能帮助我如何像你的查询一样完成上面最后的代码片段吗!!!