我正在尝试使用关系中的三个表从另一个表中搜索列。
在我看来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 = ?) )
有人可以帮助我吗?