我们需要在我们的 rails 应用程序中检索logs
for 。customer comm record
简单英语的条件是 customer_comm_record 的日志,基于:
#1. the sales_id in customer comm record's customers is equal to current user id
#2. the sales_id in customer comm record's customer's project is equal to the current user id.
#1 的 SQL 代码可以是 ( SQL#1
):
Log.joins(:customer_comm_record =>:customer).where('customers.sales_id = ?', session[:user_id])
#2 的 SQL 代码可以是 ( SQL#2
):
Log.joins(:customer_comm_record =>{ :customer => :projects}).where('projects.sales_id = ?', session[:user_id])
这是我们SQL#3
在一行中提出的 ( ) :
Log.joins(:customer_comm_record =>{ :customer => :projects}).where('customers.sales_id = ? OR projects.sales_id = ?', session[:user_id], session[:user_id])
上面返回的 SQL 代码rails console
是(将 session[:user_id] 替换为 1):
SELECT "logs".* FROM "logs" INNER JOIN "customer_comm_records" ON "customer_comm_records"."id" = "logs"."customer_comm_record_id" INNER JOIN "customers" ON "customers"."id" = "customer_comm_records"."customer_id" INNER JOIN "projects" ON "projects"."customer_id" = "customers"."id" WHERE (customers.sales_id = 1 OR projects.sales_id = 1)
问题是天气SQL#3 == SQL#1 UNION SQL#2
。有人可以回答这个问题吗?如果不是,什么是正确的?谢谢。