0

我使用 2 个选择框使用 2 个条件创建了一个搜索

  • 射精的选择框
  • 我的射精状态的选择框

选择 STATE= 0 时我的错误是没有获取值

但是当我选择 STATE =1 时

我的桌子

 TABLE EJECUTIVES
       |id|  |name| |lastname1|
 TABLE POLICIES
       |id|  |num_police|  |ejecutive_id| |state|

  state=  '0' is ok
          '1' is canceled
          '2' is expired

这是我的控制器

class PolicyManagement::PolicyController < ApplicationController

   def generate_print_ejecutive_comercial

     @ejecutives = Ejecutive.find(:all)
     @selected_ejecutive = Ejecutive.find_by_id(params[:search])  if params[:search]
     @selected_state = Policy.find_by_state(params[:state])  if params[:state]
     @search = Policy.find(:all,:conditions =>['ejecutive_id = ? AND state = ? ', @selected_ejecutive, @selected_state  ]   )
     @policies = @search.paginate( :page => params[:page], :per_page =>10)

   end

end

我的模型

class Policy < ActiveRecord::Base
   unloadable
   belongs_to :ejecutive
   has_many :policy
end

class Ejecutive < ActiveRecord::Base
  has_many :policies
end

这是我的看法,但不太了解

 <% form_tag :controller=>"policy_management/policy",:action=>"generate_print_ejecutive_comercial" do %>

 Ejecutives: 
<%= select_tag "search",options_for_select(@ejecutives.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]}, params[:search].to_i ) %>

Status:
<%= select_tag "state", options_for_select([ ["Ok",0],["Cancel",1],["ok but not cancel",2]],params[:state].to_i) %>

<%= submit_tag "search", :name => nil %>
<% end %>

Results
<% @policies.each do |policy| %>
     <p> <%= policy.num_policy%> </p>
     <p> <%= policy.ejecutive.name %> </p>
     <p> <%= policy.ejecutive.last_name %> </p>
<% end %>
<%= will_paginate @policies %>

我试过了,但只是取消了状态(1),这实际上是有效的

<%= select_tag "state", options_for_select([ ["Ok",0],["Cancel",1],["ok but not cancel",2]],params[:state].to_i) %>

仅当我选择一个 ejecutive 并且状态“1”= 取消时才显示这个

  Ejecutive Load (0.2ms)  SELECT * FROM `ejecutives` 
  Ejecutive Columns (1.7ms)   SHOW FIELDS FROM `ejecutives`
  Ejecutive Load (0.6ms)   SELECT * FROM `ejecutives` WHERE (`ejecutives`.`id` = '5') LIMIT 1
  Policy Columns (2.8ms)   SHOW FIELDS FROM `policies`
  Policy Load (0.7ms)   SELECT * FROM `policies` WHERE (`policies`.`state` = '1') LIMIT 1
  Policy Load (29.9ms)   SELECT * FROM `policies` WHERE (ejecutive_id = 5 AND state = 1 ) 

但是当我选择一个 ejecutive 并声明 ok(0) 时,这不起作用并显示这个

  Ejecutive Load (0.2ms)   SELECT * FROM `ejecutives` 
  Ejecutive Columns (1.5ms)   SHOW FIELDS FROM `ejecutives`
  Ejecutive Load (0.1ms)   SELECT * FROM `ejecutives` WHERE (`ejecutives`.`id` = '5') LIMIT 1
  Policy Columns (1.1ms)   SHOW FIELDS FROM `policies`
  Policy Load (0.3ms)   SELECT * FROM `policies` WHERE (`policies`.`state` = '0') LIMIT 1
  Policy Load (8.8ms)   SELECT * FROM `policies` WHERE (ejecutive_id = 5 AND state = 4 ) 

我不知道为什么这样做:policies当我选择状态 ok(0) 时,SELECT * FROM WHERE (ejecutive_id = 5 AND state = 4)

当我选择一个喷射和状态过期(2)时,这不起作用并显示这个

  Ejecutive Load (0.3ms)   SELECT * FROM `ejecutives` 
  Ejecutive Columns (1.5ms)   SHOW FIELDS FROM `ejecutives`
  Ejecutive Load (0.3ms)   SELECT * FROM `ejecutives` WHERE (`ejecutives`.`id` = '5') LIMIT 1
  Policy Columns (2.8ms)   SHOW FIELDS FROM `policies`
  Policy Load (6.4ms)   SELECT * FROM `policies` WHERE (`policies`.`state` = '2') LIMIT 1
  Policy Load (6.8ms)   SELECT * FROM `policies` WHERE (ejecutive_id = 5 AND state = 1098 ) 

我不知道为什么要这样做:policies当我选择 state expired(2) 时,SELECT * FROM WHERE (ejecutive_id = 5 AND state = 1098)

我将非常感谢各种帮助

4

1 回答 1

1

我认为你在逻辑上犯了一个错误,state = 1只是因为 Policy with id == 1havestate = 1

看看这个:

Policy Load (6.4ms) SELECT * FROM `policies` WHERE (`policies`.`state` = '2') LIMIT 1
Policy Load (6.8ms) SELECT * FROM `policies` WHERE (ejecutive_id = 5 AND state = 1098 ) 

在第二个查询中,您正在搜索策略,state = 1098因为您传递的@selected_statepolicy模型实例的值。

尝试将您的线路更改为:

 @search = Policy.find(:all,:conditions =>['ejecutive_id = ? AND state = ? ', @selected_ejecutive, params[:state]  ])
于 2013-09-23T06:21:48.130 回答