Search code examples
ruby-on-railsrubyruby-on-rails-2

Error in select box getting params


I created a search using 2 select box using 2 conditions

  • A select box for ejecutives
  • A select box for state of my ejecutive

My error is not getting values when select STATE= 0

But when i select STATE =1 is working

My tables

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

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

Here is my controller

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

My models

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

class Ejecutive < ActiveRecord::Base
  has_many :policies
end

This is my view but dont know much about it

 <% 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 %>

I tried this but is only getting state canceled(1) ,this is actually working

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

Is working only When i select an ejecutive and state "1" = canceled is showing this

  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 ) 

But when i select an ejecutive and state ok(0),this is not working and showing this

  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 ) 

i don't have idea why is doing: SELECT * FROM policies WHERE (ejecutive_id = 5 AND state = 4 ) when i selected state ok(0)

And when i select an ejecutive and state expired(2),this is not working and showing this

  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 ) 

i don't have idea why is doing: SELECT * FROM policies WHERE (ejecutive_id = 5 AND state = 1098 ) when i selected state expired(2)

I will really appreciate all kind of help


Solution

  • I think you make a mistake in your logic and state = 1 just because Policy with id == 1 have state = 1

    Take a look at this:

    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 ) 
    

    In the second query you are searching for policies with state = 1098 because you are passing the value of @selected_state, a policy model instance.

    Try changing your line to:

     @search = Policy.find(:all,:conditions =>['ejecutive_id = ? AND state = ? ', @selected_ejecutive, params[:state]  ])