Advertisement
Guest User

Untitled

a guest
Oct 6th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.42 KB | None | 0 0
  1. controller do
  2.     def scoped_collection
  3.       sql = <<-SQL.strip_heredoc
  4.         LEFT JOIN lots on audits.auditable_id = lots.id AND audits.auditable_type = 'Lots::Base'
  5.         LEFT JOIN bills on audits.auditable_id = bills.id AND audits.auditable_type = 'Bill'
  6.         UNION ALL
  7.         SELECT * FROM audits
  8.         LEFT JOIN lots on audits.associated_id = lots.id AND audits.associated_type = 'Lots::Base'
  9.         LEFT JOIN bills on audits.associated_id = bills.id AND audits.associated_type = 'Bill'
  10.       SQL
  11.  
  12.       Audit
  13.         .joins(sql)
  14.         .where("lots.id is not null or (bills.id is not null and bills.state != 'hidden')")
  15.         .reorder(:created_at)
  16.         .limit(LIMIT)
  17.         .preload(:auditable)
  18.         .preload(:user)
  19.     end
  20.   end
  21.  
  22. PG::SyntaxError: ERROR:  each UNION query must have the same number of columns
  23. LINE 4: SELECT * FROM audits
  24.                ^
  25. : SELECT  "audits".* FROM "audits" LEFT JOIN lots on audits.auditable_id = lots.id AND audits.auditable_type = 'Lots::Base'
  26. LEFT JOIN bills on audits.auditable_id = bills.id AND audits.auditable_type = 'Bill'
  27. UNION ALL
  28. SELECT * FROM audits
  29. LEFT JOIN lots on audits.associated_id = lots.id AND audits.associated_type = 'Lots::Base'
  30. LEFT JOIN bills on audits.associated_id = bills.id AND audits.associated_type = 'Bill' WHERE (lots.id is not null or (bills.id is not null and bills.state != 'hidden')) ORDER BY "audits"."id" desc LIMIT 30 OFFSET 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement