views:

156

answers:

2

I am using will_paginate for pagination but I can't seem to use more than one condition at a time. For example, if I want to have a sql query that ends in "Where office_id = 5", then it's pretty straight forward, I can do that. but what if I want to do "Where office_id = 5 AND primary_first = 'Mark'"? I can't do that. I have no idea how to enter multiple conditions. Can you help??

Below is an example of my code:

def self.search(search, page, office_id)
  paginate :per_page => 5, :page => page,
           :conditions => ['office_id', "%#{office_id}"], # + ' and primary_first like ?', "%#{params[:search]}%"],
           #:conditions => ['primary_first', "%#{search}%"],
           :order => 'created_at'

end

Thank you for your help!

+1  A: 

try :conditions => ["office_id = ? and primary_first = ?", office_id, search]

or using a dynamic finder:

paginate_by_office_id_and_primary_first(office_id, search, :per_page => 5, :page => page, :order => 'created_at')

These will only work for equivalency, however the db is defining it. If you need to use LIKE, see Jens Fahnenbruck's answer

x1a4
+2  A: 

If I understand correctly what you want, this should work:

def self.search(search, page, office_id)
  paginate :per_page => :page => page,
           :conditions => ["office_id LIKE ? and primary_first LIKE ?", "%#{office_id}", "%#{search}%"]
           :order => :created_at
end
jigfox