As my title, I want set multiple conditions in will_paginate, that's my model's code
class Room < ActiveRecord::Base
belongs_to :province
belongs_to :city
belongs_to :region
belongs_to :street
belongs_to :estate
def self.search(city_id, region_id, layout, page)
conditions = []
conditions.push("city_id = #{city_id}")
conditions.push("region_id = #{region_id}")
conditions.push("layout = #{layout}") if layout!=0
paginate :per_page => 5, :page => page,
:conditions => conditions
end
end
invode it in controller:
@rooms = Room.search(city, region, layout, params[:page])
but..doesn't work right..It just consider the first condition.
who can give me a sample code?? Thx!
Try this:
class Room < ActiveRecord::Base
belongs_to :province
belongs_to :city
belongs_to :region
belongs_to :street
belongs_to :estate
def self.search(city_id, region_id, layout, page)
conditions = []
conditions.push("city_id = #{city_id}")
conditions.push("region_id = #{region_id}")
conditions.push("layout = #{layout}") if layout!=0
paginate :per_page => 5, :page => page,
:conditions => conditions.join(' and ')
end
end
This would concatenate all of your conditions with "AND".
The reason your code doesn't work is that when you use an Array for condition, the first element should be a condition with SQL params and remaining elements - are param values. For example:
Room.paginate(:conditions => ['city_id = ? and region_id = ?', city_id, region_id])
That's why it only considers the first condition.