Rails比较同一列

时间:2013-12-19 01:07:29

标签: ruby-on-rails ruby-on-rails-3.2

我有一个搜索应用程序,我需要比较数据库中同一列的两个变种,但我当前的代码只注意到第二个选项。所以在下面的代码中,如果留在选定的选项,它只搜索多种选项“2255”。

表格

<%= form_tag vpc_search_path do %>
<%= select_tag :variety_one, options_from_collection_for_select(@variety, "variety_id", "variety_name", :selected => "2000"), include_blank: false %>

<%= select_tag :variety_two, options_from_collection_for_select(@variety, "variety_id", "variety_name", :selected => "2255"), include_blank: false %>
<% end %>

控制器

 def index
    all = Result.select(:variety_id)
    @variety = Variety.where(:variety_id => all).order('variety_name DESC')
  end

  def search
   if params[:variety_one] != params[:variety_two]
   @comparison = Result.where('variety_id' => params[:variety_one], 'variety_id' => params[:variety_two])
   else
   redirect_to vpc_index_url, notice: "Can't compare the same variety"
   end
  end

1 个答案:

答案 0 :(得分:1)

我不认为在这样的哈希中为相同的属性/列添加2个条件是有效的。相反,您可以为一个属性/列传递值数组,因此请将查询更改为:

@comparison = Result.where('variety_id' => [params[:variety_one], params[:variety_two]])

Query Interface Rails Guide是我一直在寻找如何进行此类查询的地方。