Consider the 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, what should be the value that must be returned for Arel to ignore the AND clause effect?
Also, is there a better way to handle this case?
It doesn't seem to be possible to give any arguments to
and
that cause it to do nothing. However, you can just calland
conditionally:It's extremely clunky, but it gets the job done.