0

There are 3 tables payment_logs, sourcings and purchasings in our rails app. A payment_log belongs to either sourcing or purchasing but not both at the same time. There is a col project_id in both sourcing and purchasing. We want to pick up all payment_logs with its project_id = project_id_search (project_id_search passed from a search page). Also we need a ActiveRecord as resultset returned. Here is the individual query, assuming payment_logs holds the ActiveRecord result set:

  1. pick all payment_logs with its sourcing's project_id = project_id_search

    payment_logs = payment_logs.joins(:sourcing).where("sourcings.project_id = ?", project_id_search)

  2. pick all payment_logs with its purchasing's project_id = project_id_search

    payment_logs = payment_logs.(:purchasing).where("purchasings.project_id = ?", project_id_search)

We need to union 1 and 2 in order to pick up all the payment_logs whose project_id = project_id_search. What's the right way to accomplish it? We did not find union in rails and find_by_sql returns an array which is not what we want. Thanks.

1 Answer 1

1
payment_logs.where(["
  payment_logs.sourcing_id IN (
                                SELECT id FROM sourcings WHERE sourcings.project_id = ? 
                              ) 
  OR payment_logs.purchasing_id IN 
                              (
                                SELECT id FROM purchasings WHERE purchasings.project_id = ? 
                              )", project_id_search, project_id_search])

Lot of SQL, but it should work

Option 2 (two SQL requests ...) :

payment_logs = []
payment_logs << PaymentLog.joins(:sourcing).where("sourcings.project_id" => project_id_search)
payment_logs << PaymentLog.joins(:purchasing).where("purchasings.project_id" => project_id_search)
payment_logs.uniq! #In case some records have both a sourcing and a purchasing

Option 3, with the squeel gem : https://github.com/ernie/squeel

PaymentLog.where{(source_id.in Sourcing.where(:project_id => project_id_search)) | (purchasing_id.in Purchasing.where(:project_id => project_id_search))}

I like this solution :) Also, whenever you have a doubt on the generated SQL, from the console or anywhere else, you can add .to_sql at the end of an ActiveRecord query to double check the generated SQL

9
  • Works. Thanks. Is there a way to replace the SELECT with rails flavor query? Something like Sourcing.where("project_id = ?", project_id_search).select("id"). I got bunch of errors with the replacement.
    – user938363
    Jul 30, 2012 at 20:31
  • I tried to find a better way to do it, but couldn't find much ... I think you have to stick to this ugly SQL syntax or if you're willing to do more than one SQL request, you could do both requests separately and then merging the two result sets Jul 30, 2012 at 20:35
  • I was trying to merge but did not work out. I came to the same idea as you posted. Tried to implement in rails way. Was using the range like :sourcing_id => [sourcing_ids] OR :purchasing_id => [purchasing_ids] and no success. Tried where("sourcing_id IN ? OR purchasing_id IN ?", [sourcing_ids],[purchasing_ids] ). Both did not work out.
    – user938363
    Jul 30, 2012 at 21:01
  • Added option 2 to my answer, you can try it :) Jul 30, 2012 at 21:07
  • what's the purpose of [] in where?
    – user938363
    Jul 30, 2012 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.