Raony Vieira ferreira wrote:
> I don't need both, I tried but I had no success using generic
> I think I've done something wrong in model.

Skinny controllers, fat models.

Given scoping criteria that may be coming in params, the model should 
know how to respond to those params for limiting return sets, and can 
adapt its 'cond' to account for those.  Controller just knows to use the 
received cond in its paginate call.

Model:

class Contract
  def build_conditions(params)
    # the default returns
    cond = ['1=1']
    page = (params[:page] ||= 1).to_i
    per_page = 15
    if params[:partial_contract_name]
      # return a scoping condition here, this can be as complex
      # as you want/require
      cond = ['name LIKE ?%', params[:partial_contract_name]
    end
    return cond, page, per_page
  end
end

Controller:

  def index
    @contract = Contract.new(params[:contract])
    cond, page, per_page = @contract.build_conditions(params)
    @contracts = Contract.paginate :conditions => cond, :page => page, 
:per_page => per_page
    @legal_entities = LegalEntity.all(:select => "CD_PESSOA_JURIDICA,
NM_PESSOA", :joins => [:person])
    @persons = Person.all
    respond_to do |format|
      format.html # index.html.erb
    end
  end

should be a nice generic implementation using will_paginate.  Again, I 
don't know what was happening in your search_index or find_by_params 
methods.
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to