【发布时间】:2015-06-20 07:00:34
【问题描述】:
考虑以下代码片段:
def sql
billing_requests
.project(billing_requests[Arel.star])
.where(
filter_by_day
.and(filter_by_merchant)
.and(filter_by_operator_name)
)
.to_sql
end
def filter_by_day
billing_requests[:created_at].gteq(@start_date).and(
billing_requests[:created_at].lteq(@end_date)
)
end
def filter_by_operator_name
unless @operator_name.blank?
return billing_requests[:operator_name].eq(@operator_name)
end
end
def filter_by_merchant
unless @merchant_id.blank?
return billing_requests[:merchant_id].eq(@merchant_id)
end
end
private
def billing_requests
@table ||= Arel::Table.new(:billing_requests)
end
在方法filter_by_merchant中,当商户ID为空时,Arel必须返回什么值才能忽略AND子句的影响?
另外,有没有更好的方法来处理这种情况?
【问题讨论】:
-
据我所知,没有任何参数可以传递给
and使其什么也不做。 -
你试过“return billing_requests.all”吗?
标签: ruby-on-rails ruby activerecord arel