我正在使用Rails3测试版,will_paginate gem和geokit gem&插件。
作为geokit-rails插件,似乎不支持Rails3范围(包括:origin符号是问题),我需要使用.find语法。
代替范围,我需要以数组格式组合两组标准:
我有一个默认条件:
conditions = ["invoices.cancelled = ? AND invoices.paid = ?", false, false]
我可能需要将以下条件之一添加到默认条件,具体取决于UI选择:
#aged 0
lambda {["created_at IS NULL OR created_at < ?", Date.today + 30.days]}
#aged 30
lambda {["created_at >= ? AND created_at < ?", Date.today + 30.days, Date.today + 60.days]}
#aged > 90
lamdba {["created_at >= ?", Date.today + 90.days]}
生成的查询类似于:
@invoices = Invoice.find(
:all,
:conditions => conditions,
:origin => ll #current_user's lat/lng pair
).paginate(:per_page => @per_page, :page => params[:page])
问题:
感谢您的时间。
答案 0 :(得分:3)
试试这个:
ca = [["invoices.cancelled = ? AND invoices.paid = ?", false, false]]
ca << ["created_at IS NULL OR created_at < ?",
Date.today + 30.days] if aged == 0
ca << ["created_at >= ? AND created_at < ?",
Date.today + 30.days, Date.today + 60.days] if aged == 30
ca << ["created_at >= ?", Date.today + 90.days] if aged > 30
condition = [ca.map{|c| c[0] }.join(" AND "), *ca.map{|c| c[1..-1] }.flatten]
编辑方法2
Monkey修补Array
类。在monkey_patch.rb
目录中创建名为config/initializers
的文件。
class Array
def where(*args)
sql = args[0]
unless (sql.is_a?(String) and sql.present?)
return self
end
self[0] = self[0].present? ? " #{self[0]} AND #{sql} " : sql
self.concat(args[1..-1])
end
end
现在你可以这样做:
cond = []
cond.where("id = ?", params[id]) if params[id].present?
cond.where("state IN (?)", states) unless states.empty?
User.all(:conditions => cond)
答案 1 :(得分:2)
我认为更好的方法是使用匿名范围 看看这里: http://railscasts.com/episodes/112-anonymous-scopes