使用关系的条件问题

时间:2013-10-02 17:17:20

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

我正在尝试使用关系中的三个表来搜索另一个表中的列。

在我看来policy_vehicles我有一个text_field_tag,想要按“raz_soc”进行搜索。

我的表是:

Policy_vehicles
   |id|       |policy_id| 
    integer      integer
     100            1 
     200            2

Policies
   |id|    |client_id|
   integer  integer
    1        1
    2        2     

Clients
   |id|     |raz_soc|
    integer  varchar(255)
    1       MARATEX SAC
    2       ATT

这是我的控制者:

class PolicyManagement::PolicyController < ApplicationController

  def generate_print_per_vehicle
      params[:search_policy_id] =   Policy.find(:all,:joins => :client ,:conditions => ['raz_soc LIKE ?',"%#{params[:search_raz_soc]}%"  ])
      @policies= PolicyVehicle.find(:all,:joins => :policy, :conditions => ['policy_id = ?',params[:search_policy_id] ]) 
  end

end

这是我的模特:

class Policy < ActiveRecord::Base
  belongs_to :client
  has_many :policy_vehicles
end

class PolicyVehicle < ActiveRecord::Base
  belongs_to :policy
end

class Client < ActiveRecord::Base
  has_many :policies
end

这是我的观点,我试图通过另一个表中的列找到:

<% form_tag :controller=>"policy_management/policy",:action=>"generate_print_per_vehicle" do %>
 Society:
   <%= text_field_tag "search_raz_soc",params[:search_raz_soc] %>
   <%= submit_tag "Buscar", :name => nil %>
<% end %>

我的日志是:

  Mysql::Error: Operand should contain 1 column(s): SELECT `policy_vehicles`.* FROM `policy_vehicles`   INNER JOIN `policies` ON `policies`.id = `policy_vehicles`.policy_id  WHERE (policy_id = 166,1540,2822,4074)

它应该是这样的搜索:

 Policy Load (3.1ms)   SELECT `policies`.* FROM `policies` INNER JOIN `clients` ON `clients`.id = `policies`.client_id WHERE (raz_soc = 'MARATEX SAC') 
 PolicyVehicle Load (0.3ms)   SELECT `policy_vehicles`.* FROM `policy_vehicles` INNER JOIN `policies` ON `policies`.id = `policy_vehicles`.policy_id WHERE (policy_id = 1) 

我试过这个但是没有用:

 #controller
 @policy =         Policy.find(:all,:joins => :client ,:conditions => ['raz_soc LIKE ?',params[:search_raz_soc]  ])
 @policies= PolicyVehicle.find(:all,:joins => :policy, :conditions => ['policy_id = ?',@policy    ])

#logs
 Policy Load (3.1ms)   SELECT `policies`.* FROM `policies` INNER JOIN `clients` ON `clients`.id = `policies`.client_id WHERE (raz_soc = 'MARATEX SAC') 
 PolicyVehicle Load (0.3ms)   SELECT `policy_vehicles`.* FROM `policy_vehicles` INNER JOIN `policies` ON `policies`.id = `policy_vehicles`.policy_id WHERE (policy_id = 70353610714140) 

我正在尝试做这样的事情

 select * from policy_vehicles where policy_id
    IN ( SELECT id FROM policies WHERE
    client_id IN (SELECT id FROM clients raz_soc = ?) )

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

试试这个(我已经解释了查询返回的内容,所以请确保这是你想要的)

@policies =  Policy.find(:all,:joins => :client ,
             :conditions => ['raz_soc LIKE ?',"%#{params[:search_raz_soc]}%"] ) 
# returns an array of policy records based on the raz_soc condition on client 

@policy_vehicles = PolicyVehicle.find(:all,:joins => :policy,
                   :conditions => ['policy_id IN (?)',@policies] )
# returns an array of policy_vehicles that are associated with any record in the @policies array

其他选项是在单个查询中执行:

@policy_vehicles = PolicyVehicle.joins(:policy => :client).
                   where('clients.raz_soc LIKE ?',"%#{params[:search_raz_soc]}%")