我使用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) %>
仅工作当我选择一个连续并且状态“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 )
但是当我选择一个连续并且状态确定(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 )
我不知道为什么这样做:SELECT * FROM policies
WHERE(ejecutive_id = 5 AND state = 4)当我选择状态ok(0)
当我选择一个永久性和状态已过期(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 )
我不知道为什么这样做:SELECT * FROM policies
WHERE(ejecutive_id = 5 AND state = 1098)当我选择状态已过期时(2)
我将非常感谢所有的帮助
答案 0 :(得分:1)
我认为你的逻辑错误和state = 1
只是因为id == 1
的政策有state = 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_state
policy
模型实例的值。
尝试将您的行更改为:
@search = Policy.find(:all,:conditions =>['ejecutive_id = ? AND state = ? ', @selected_ejecutive, params[:state] ])