关于多个条件的 will_paginate 的问题
正如我的标题,我想在 will_paginate 中设置多个条件,这是我的模型代码
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
在控制器中调用它:
@rooms = Room.search(城市, 区域、布局、参数[:page])
但是..不起作用正确..它只考虑第一个条件。
谁能给我一个示例代码??谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好的方法是使用这样的条件(Rails 中首选)
这将消除 SQL 注入的风险。
A better approach would be to use conditions like this(which is preferred in Rails)
This will eliminate the risk of SQL injection.
试试这个:
这会将所有条件用“AND”连接起来。
您的代码不起作用的原因是,当您使用数组作为条件时,第一个元素应该是带有 SQL 参数的条件,其余元素是参数值。例如:
这就是为什么它只考虑第一个条件。
Try this:
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:
That's why it only considers the first condition.