0

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

在我看来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 = ?) )

有人可以帮助我吗?

4

1 回答 1

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]}%")
于 2013-10-02T19:13:23.943 回答