ruby-on-rails – Arel AND子句和Empty条件
发布时间:2020-12-16 23:32:38 所属栏目:百科 来源:网络整理
导读:请考虑以下代码片段: def sql billing_requests .project(billing_requests[Arel.star]) .where( filter_by_day .and(filter_by_merchant) .and(filter_by_operator_name) ) .to_sqlenddef filter_by_day billing_requests[:created_at].gteq(@start_date).
请考虑以下代码片段:
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子句效果? 解决方法
你应该返回true.当你运行.and(true)时,它最终会在sql中转换为’AND 1′.
def filter_by_merchant return true if @merchant_id.blank? billing_requests[:merchant_id].eq(@merchant_id) end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |