ruby on rails - Arel AND clause and Empty condition -
consider following code fragment:
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
in method filter_by_merchant
when merchant id becomes empty, should value must returned arel ignore , clause effect? also, there better way handle case?
you should return true. when run .and(true) converted 'and 1' in sql.
def filter_by_merchant return true if @merchant_id.blank? billing_requests[:merchant_id].eq(@merchant_id) end
Comments
Post a Comment