我正在尝试将查询限制为仅返回属于登录用户且在日期范围内的结果。这是范围:
scope :unpaid, ->(start_date, end_date, current_user) { joins('LEFT OUTER JOIN carrier_commissions ON
dealer_commissions.phone_id = carrier_commissions.phone_id AND
dealer_commissions.name = carrier_commissions.name')
.where('carrier_commissions.id IS NULL').where('customer_id: current_user.customer.id, created_at: start_date..end_date') }
def self.reconciliation(start_date, end_date, current_user)
...
dealer_commissions.unpaid.each do |unpaid|
unpaid.update(status: "Unpaid", discrepancy_balance: unpaid.payment)
if unpaid.initial_discrepancy.blank?
unpaid.update(initial_discrepancy: unpaid.payment)
end
end
end
这里是reconcile
控制器动作,它具有实际参数:
def reconcile
start_date = Date.new(params[:range][:"start_date(1i)"].to_i,params[:range][:"start_date(2i)"].to_i,params[:range][:"start_date(3i)"].to_i)
end_date = Date.new(params[:range][:"end_date(1i)"].to_i,params[:range][:"end_date(2i)"].to_i,params[:range][:"end_date(3i)"].to_i)
DealerCommission.reconciliation(start_date, end_date, current_user)
redirect_to dealer_commissions_url, notice: 'Dealer commissions reconciled.'
end
这是我的日志中的示例:
SELECT COUNT(*) FROM "dealer_commissions" LEFT OUTER JOIN carrier_commissions ON
dealer_commissions.phone_id = carrier_commissions.phone_id AND
dealer_commissions.name = carrier_commissions.name WHERE "dealer_commissions"."customer_id" = ? AND ("dealer_commissions"."created_at" BETWEEN '2015-02-03' AND '2015-06-03') AND (carrier_commissions.id IS NULL) AND (customer_id: current_user.customer.id, created_at: start_date..end_date) [["customer_id", 8]]
SQLite3::SQLException: unrecognized token: ":": SELECT COUNT(*) FROM "dealer_commissions" LEFT OUTER JOIN carrier_commissions ON
dealer_commissions.phone_id = carrier_commissions.phone_id AND
dealer_commissions.name = carrier_commissions.name WHERE "dealer_commissions"."customer_id" = ? AND ("dealer_commissions"."created_at" BETWEEN '2015-02-03' AND '2015-06-03') AND (carrier_commissions.id IS NULL) AND (customer_id: current_user.customer.id, created_at: start_date..end_date)
目标是获取具有这些条件的记录(start date, end date, current_user
)。
答案 0 :(得分:0)
您需要更正语法。目前你正在将一个静态字符串传递给mysql,不包括你的rails变量。
.where('customer_id = ? AND created_at > ? AND created_at < ?', current_user.customer.id, start_date, end_date)