当我在搜索中有很多参数时,如何简化条件?

时间:2013-11-21 22:13:45

标签: ruby-on-rails ruby ruby-on-rails-2

例如

我的搜索视图中有5个参数:

  params[:a]       params[:b]       params[:c]       params[:d]       params[:e]

我希望在以下情况下搜索我的控制器:

  params[:a].present? and params[:b].nil?
  params[:a].present? and params[:c].nil?
  params[:a].present? and params[:e].nil?
  params[:b].present? and params[:c].nil?
  params[:b].present? and params[:d].nil?
   ............
  params[:a].nil? and params[:b].nil?
  .............

以下是我的尝试:

 controller: 

 if params[:a].nil?
    @query
 else
    if params[b].nil?
      @another_query
    else
      ...........#and it will continue with lots of conditions according with params
    end
 end

我有很多条件可以尝试

这是使用一个真实的例子我得到了这个错误“未定义的方法`paginate'为nil:NilClass”

 #table
 policies
   id   num_policy   type_id    doc    name
    1        2323      1       181844  Charles
    2        2121      2       151511  Aguardientico
    3        3423      2       434344  Benjamin

 #controller

if params[:num_policy].present?
  @search= Policy.find(:all,:conditions=>['num_policy LIKE ?',params[:num_policy] ])
end

if params[:type_id].present?
  @search= Policy.find(:all,:conditions=>['type_id LIKE ?',params[:type_id] ])
end

if params[:doc].present?
  @search= Policy.find(:all,:conditions=>['doc LIKE ?',params[:doc] ])
end

if params[:name].present?
  @search= Policy.find(:all,:conditions=>['name LIKE ?',params[:name] ])
end

if params[:doc].present? and params[:type_id].present?
  @search= Policy.find(:all,:conditions=>['doc LIKE ? and type_id LIKE ?',params[:doc],params[:type_id] ])
end

@policies= @search.paginate( :page => params[:page], :per_page =>2)

#view

<% form_tag :controller=>"policy",:action=>"search" do %>
   <%= text_field_tag "num_policy",params[:num_policy] %>
   <%= text_field_tag "doc",params[:doc] %
   <%= text_field_tag "name",params[:name] %
   <%= text_field_tag "type_id",params[:type_id] %
<% end %> 

 <% @policies.each do |p| %>
   <%= p.num_policy %>
   <%= p.type_id %>
   <%= p.doc %>
   <%= p.name %>
 <% end %>

但是如果删除paginate我得到了这个错误“当你没想到它时你有一个nil对象! 您可能期望一个Array实例。 在评估nil.each“

时发生错误
 #controller
 @policies= @search

 #says that my line  <% @policies.each do |p| %>  is nil

有没有办法控制它们?

我只是想要意见,也许有人也发生了这个......

感谢。

3 个答案:

答案 0 :(得分:2)

@search = Policy.scoped
@search = @search.where('num_policy LIKE ?', params[:num_policy]) if params[:num_policy].present?
@search = @search.where('type_id LIKE ?', params[:type_id]) if params[:type_id].present?
@search = @search.where('doc LIKE ?', params[:doc]) if params[:doc].present?
@search = @search.where('name LIKE ?', params[:name]) if params[:name].present?
@policies = @search.paginate(page: params[:page], per_page: 2)

BTW:问题是因为你在if语句中创建了@search,所以如果没有params,那么@search就不存在了。

这是rails 2版本:

conditions_str = []
conditions_values = {}
['num_policy', 'type_id', 'doc', 'name'].each do |cond|
  if params[cond.to_sym].present?
    conditions_str << "#{cond} LIKE :#{cond}"
    conditions_values[cond.to_sym] = params[cond.to_sym]
  end
end
@policies = Policy.find(:all, :conditions => [conditions_str.join(" AND "), conditions_values])

答案 1 :(得分:1)

您需要清理params并仅使用搜索条件提取哈希值,然后您就可以编写简单易读的条件。例如:

# define what are the search keys that can be found in params[]
search_keys = [:a, :b, :c, :d, :e, :f]

# extract hash values that concern only the search criteria
search_params = params.select { |k,v| search_keys.include? k and ! v.nil? and ! v.empty? }

# Write query according to search criteria presents
case search_params.keys.sort
  when [:a]
    # Write the query when only :a search criteria is present
    Something.where("blahblah = ?", params[:a])
  when [:a, :c]
    # Write the query when only :a and :c search criteria are present
    Something.where("a_field = ? AND stuff = ?", params[:a], params[:c])
  when ...
    ...
end

答案 2 :(得分:0)

我真的不知道我是否理解你的问题,但无论如何我都会发布我的答案。

我会做一个功能:

def simplified_conditions conds, prms, perform_and_on_hashes = false
  conds.each do |h|
    h.each do |method_name, symbols_array|
      symbols_array.each do |symb|
        return false unless prms[symb].send method_name
      end
    end
    return true unless perform_and_on_hashes
  end
  return true
end

并传递一个看起来像这样的条件结构:

[
  { 'boolean_function_name' => [:array,:of,:symbols],
    'another_function_name' => [:array,:of,:symbols],
    'another_function_name' => [:array,:of,:symbols]
  },
  { 'boolean_function_name' => [:array,:of,:symbols],
    'another_function_name' => [:array,:of,:symbols],
    'another_function_name' => [:array,:of,:symbols]
  },
  { 'boolean_function_name' => [:array,:of,:symbols],
    'another_function_name' => [:array,:of,:symbols],
    'another_function_name' => [:array,:of,:symbols]
  }
]

因此,在您的示例条件中,数组将如下所示:

conds = [
  { 'present?' => [:a], 'nil?' => [:b] },
  { 'present?' => [:a], 'nil?' => [:c] },
  { 'present?' => [:a], 'nil?' => [:e] },
  { 'present?' => [:b], 'nil?' => [:c] },
  { 'present?' => [:b], 'nil?' => [:d] }
]

# And then to call it:
final_boolean_result = simplified_conditions(conds, params, false)

如果示例中的任何行为true,则返回true。我希望所有这些都是真的,只需将最后一个参数更改为true

如果我误解了你的问题,请发表评论。