Topic: problems at moving a special query from controller to model

Hello,

i want to refactor und DRY up my Programm. My goal was to move the search logic into my Shk Model. I have tried it... The best thing i got to work was a result of all objects(like a find: all ;-) So if someone could help me to realize this i would be really glad.
The second thing is that if the find is working out of the model i want paginate the search result(will_paginate). But one step after another...


my Shk.rb (model)

require_dependency "search"
class Shk < ActiveRecord::Base
searches_on :all
  has_and_belongs_to_many :shkcompanies,
                          :join_table => "shkcompanies_shks",
                          :foreign_key => "shk_id"                         
  has_and_belongs_to_many :shkcountries,
                          :join_table => "shkcountries_shks",
                          :foreign_key => "shk_id"
  has_and_belongs_to_many :shkarticlecategories,
                          :join_table => "shkarticlecategories_shks",
                          :foreign_key => "shk_id"
  has_and_belongs_to_many :shkproductcategories,
                          :join_table => "shkproductcategories_shks",
                          :foreign_key => "shk_id"
                                                   
  validates_presence_of :title, :article, :volume, :issue
  validates_uniqueness_of :title
 
  def self.find_all(options = {})
    with_scope :find => options do
      find(:all)
    end
  end

  def self.find_volume
    find(:all, :group => 'volume', :order => 'volume DESC')
  end
  def self.find_issue
    find(:all, :group => 'issue')
  end
end


my Shk-Controller

  def searchform
    @volume = Shk.find_volume
    @issue = Shk.find_issue
    @shkarticlecategories = Shkarticlecategory.find_articlecategory
    @shkproductcategories = Shkproductcategory.find_productcategory
    @concern = Shkcompany.find_concern
    @country = Shkcountry.find_country
  end

  def customquery
    if !((params[:concern].empty?) && (params[:land].empty?) && (params[:volume].empty?) && (params[:articlecategory].empty?) && (params[:productcategory].empty?) && (params[:issue].empty?))
      filled_fields = %w{ concern land volume articlecategory productcategory issue }.reject {|f| params[f].nil? || params[f].empty?}
      conditions = [
      filled_fields.map {|f| f+' = ?'}.join(' and '),
      *filled_fields.map {|f| params[f]}
    ]
      @searchresult = Shk.find :all, :conditions => conditions, :include => [:shkcompanies, :shkcountries, :shkarticlecategories, :shkproductcategories]
      render :action => "searchresult"
    else
      @searchresult = []
      render :action => "searchresult"
    end
  end


my searchform.rhtml

<h1>Suchformular</h1>

<% form_for :shk, :method => 'get', :url => {:action => 'customquery'} do |searchform| %>
  <%= render :partial => 'searchform', :locals => {:searchform => searchform} %>
  <%= submit_tag "Suche starten", :name => nil %>
<br/>
<% end %>
<br/>
<%= link_to 'Suchformlar zuruecksetzen', :action => 'searchform', :method => 'get' %>&nbsp;&nbsp;&nbsp;
<%= link_to 'Back', :action => 'list' %>


my _searchform.rhtml partial

<!--[searchform:shk]-->

    <p>Firma:
    <select id="concern" name="concern">
        <option value=""></option>
        <%= options_from_collection_for_select @concern, :concern, :concern %>
    </select></p></br>

    <p>Land:
    <select id="land" name="land">
        <option value=""></option>
        <%= options_from_collection_for_select @country, :land, :land %>
    </select></p></br>
   
    <p>Jahr:
    <select id="volume" name="volume">
        <option value=""></option>
        <%= options_from_collection_for_select @volume, :volume, :volume %>
    </select></p></br>
   
    <p>Ausgabe:
    <select id="issue" name="issue">
        <option value=""></option>
        <%= options_from_collection_for_select @issue, :issue, :issue %>
    </select></p></br>
   
    <p>Artikelkategorie:
    <select id="articlecategory" name="articlecategory">
        <option value=""></option>
        <%= options_from_collection_for_select @shkarticlecategories, :articlecategory, :articlecategory %>
    </select></p></br>   
   
    <p>Produktkategorie:
    <select id="productcategory" name="productcategory">
        <option value=""></option>
        <%= options_from_collection_for_select @shkproductcategories, :productcategory, :productcategory %>
    </select></p></br>

   
<!--[searchform:shk]-->