関係によって2つのテーブルを使用して検索を作成しようとしています:
1)ビュー「ポリシー」で「select_tag」を使用してすべてのエジェクティブを選択します。
2)テーブル「Ejecutives」からエジェクティブを選択した後、このように選択したエジェクティブごとのすべてのポリシーが必要です。
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] %>
誰かがこの問題について知っていますか?助けていただければ幸いです。