0

大家好,我正在尝试使用 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 )  %>

请有人可以帮助我吗?我将非常感谢帮助

4

2 回答 2

1

如果您使用:all指定的 find,即使该数组的大小为 1,您也会得到一组@vehicle.id车辆@vehicle[0].id

PolicyVehicle.find(:all, :conditions => ['vehicle_id = ?  ', @vehicle[0].id])

但是,如果您不需要该@motor数组用于任何其他目的,您应该可以使用一个查询:join直接转到您想要的记录:

PolicyVehicle.find(:all, :joins => :vehicle, :conditions => ['motor =?',params[:search_motor]])
于 2013-10-01T19:53:17.263 回答
1

如果您针对 VARCHAR(255) “搜索”,您可能希望使用LIKE而不是=.

@vehicle = Vehicle.find(:all,:conditions => ['motor LIKE ?', "%#{params[:search_motor]}%" ])
@policies= PolicyVehicle.find(:all, :conditions => ['vehicle_id= ?', @vehicle.id]) 

此外,如果您正在运行 Rails 3 或 4,您可能希望停止使用find(:all, ...),而是使用where(..).

http://guides.rubyonrails.org/active_record_querying.html#hash-conditions

2.3.1 平等条件

Client.where(locked: true) 字段名也可以是字符串:

Client.where('locked' => true) 在belongs_to 关系的情况下,如果使用Active Record 对象作为值,则可以使用关联键来指定模型。此方法也适用于多态关系。

这是日志:

 Vehicle Load (15.0ms)   SELECT * FROM `vehicles` WHERE (motor = '%M16A1418981%') 
 PolicyVehicle Load (0.3ms)   SELECT * FROM `policy_vehicles` WHERE (vehicle_id= 70353666460940) 
于 2013-10-01T19:37:26.257 回答