关于多个条件的 will_paginate 的问题

发布于 2024-10-27 06:19:53 字数 682 浏览 1 评论 0原文

正如我的标题,我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零時差 2024-11-03 06:19:53

更好的方法是使用这样的条件(Rails 中首选)

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.merge!(:city_id => city_id)
    conditions.merge!(:region_id => region_id)
    conditions.merge!(:layout => layout) if layout!=0
    paginate :per_page => 5, :page => page,
      :conditions => conditions
  end
end

这将消除 SQL 注入的风险。

A better approach would be to use conditions like this(which is preferred in Rails)

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.merge!(:city_id => city_id)
    conditions.merge!(:region_id => region_id)
    conditions.merge!(:layout => layout) if layout!=0
    paginate :per_page => 5, :page => page,
      :conditions => conditions
  end
end

This will eliminate the risk of SQL injection.

恋竹姑娘 2024-11-03 06:19:53

试试这个:

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

这会将所有条件用“AND”连接起来。

您的代码不起作用的原因是,当您使用数组作为条件时,第一个元素应该是带有 SQL 参数的条件,其余元素是参数值。例如:

Room.paginate(:conditions => ['city_id = ? and region_id = ?', city_id, region_id])

这就是为什么它只考虑第一个条件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文