我正在尝试使用 2 个表按关系创建搜索,其中:
1)我在我的视图“政策”中使用“select_tag”选择我所有的ejecutives。
2)从我的表“Ejecutives”中选择一个 ejecutive 后,我想要我这样选择的 ejecutive 的所有政策。
select* from policies where ejecutive_id = 1
select* from policies where ejecutive_id = 2
select* from policies where ejecutive_id = "the ejecutive that i selected"
我的桌子
TABLE EJECUTIVES
|id| |name| |lastname1|
TABLE POLICIES
|id| |num_police| |ejecutive_id|
我的模型
class Policy < ActiveRecord::Base
unloadable
belongs_to :ejecutive
has_many :policy
def self.search(search)
if search
find(:all, :conditions => ["ejecutive_id LIKE ? ", "#{search}" ] )
else
find(:all)
end
end
end
class Ejecutive < ActiveRecord::Base
has_many :policies
end
这是我的控制器
class PolicyManagement::PolicyController < ApplicationController
def generate_print_ejecutive_comercial
@ejecutives = Ejecutive.find(:all)
@policies = Policy.search(params[:search]).paginate(:page => params[:page], :per_page => 10)
end
end
这是我的看法
<% form_tag :controller=>"policy_management/policy",:action =>"generate_print_ejecutive_comercial", :method => 'get' do %>
<%= select_tag "Ejecutives", options_for_select(@ejecutives.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]}) %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Results
<% @policies.each do |policy| %>
<p> <%= policy.num_policy%> </p>
<p> <%= policy.ejecutive.name %> </p>
<p> <%= policy.ejecutive.last_name %> </p>
<% end %>
<%= will_paginate @policies %>
我试过这个
%= select_tag "Ejecutives", options_for_select(@ejecutives.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]}),params[:search] %>
有人知道这个问题吗?我真的很感激帮助。