大家好,我正在尝试使用 text_field_tag 从另一个表中搜索 id 隐藏的列
在我看来 policy_vehicles 我有 text_field_tag 并想按电机搜索
我的桌子
Vehicles
|id| |motor| |plate|
integer varchar(255) varchar(255)
1 MOTOR1 PLATE1
2 MOTOR2 PLATE2
3 MOTOR3 PLATE3
Policy_vehicles
|id| |vehicle_id|
integer integer
1 1
2 2
3 3
4 2
这是我的控制器
class PolicyManagement::PolicyController < ApplicationController
def generate_print_per_vehicle
@motor =Vehicle.find(:all,:conditions=>['motor =?' ,params[:search_motor] ] )
@policies= PolicyVehicle.find(:all,:conditions=>['vehicle_id= ?',params[:search_motor] ] )
end
end
这是我的模型
class PolicyVehicle < ActiveRecord::Base
belongs_to :vehicle
end
class Vehicle < ActiveRecord::Base
has_many :policy_vehicles
end
这是我试图通过另一个表中的列查找的视图
<% form_tag :controller=>"policy_management/policy",:action=>"generate_print_per_vehicle" do %>
Motor:
<%= text_field_tag "search_motor",params[:search_motor] %>
<%= submit_tag "Buscar", :name => nil %>
<% end %>
我的日志
Vehicle Load (0.1ms) SELECT * FROM `vehicles` WHERE (motor ='4D56UCCR5811')
PolicyVehicle Load (0.3ms) SELECT * FROM `policy_vehicles` WHERE (vehicle_id= '4D56UCCR5811')
应该是这样的搜索
SELECT * FROM `vehicles` WHERE (motor ='4D56UCCR5811' and id =1)
SELECT * FROM `policy_vehicles` WHERE (vehicle_id= '1')
我试过这个但不工作
#controller
@vehicle = Vehicle.find(:all,:conditions=>['motor =?' ,params[:search_motor] ])
@policies = PolicyVehicle.find(:all, :conditions => ['vehicle_id = ? ', @vehicle.id])
#logs
Vehicle Load (17.2ms) SELECT * FROM `vehicles` WHERE (motor ='4D56UCCR5811')
PolicyVehicle Load (0.3ms) SELECT * FROM `policy_vehicles` WHERE (vehicle_id = 70353663367520 )
好吧,我做了一个 select_tag,但我想写电机,而不是像这样选择
<%= select_tag "search_motor",options_for_select(@vehicle.collect {|t| [t.motor.to_s ,t.id]}, params[:search_motor].to_i ) %>
请有人可以帮助我吗?我将非常感谢帮助