Guest User

Untitled

a guest
Oct 15th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.08 KB | Source Code | 0 0
  1. # Base query
  2. ransack_query = Interaction.ransack(interactions_event_query).result
  3.  
  4. emp_ids = ransack_query.pluck(Arel.sql("(metadata->>'employee_id')::INTEGER")).uniq
  5.  
  6. # employee_searches is a materialized view and it doesn't have an employee_id column which can be used as a default in the join, Also the number of rows can be large
  7. employees = "SELECT distinct id, #{compare_key} FROM employee_searches where id in (#{emp_ids.join(',')})"
  8.  
  9. # interactions table has a JSONB column named metadata which has a key named employee_id
  10. total_interactions = ransack_query
  11.     .select("interaction_type, (metadata->>'employee_id')::INTEGER as employee_id")
  12.     .joins("LEFT JOIN (#{employees}) AS employees ON interactions.employee_id = employees.id")
  13.     .group("interaction_type", "employees.#{compare_key}")
  14.     .count
  15.  
  16. unique_interactions = ransack_query
  17.     .select("distinct interaction_type, (metadata->>'employee_id')::INTEGER as employee_id")
  18.     .joins("LEFT JOIN (#{employees}) AS employees ON interactions.employee_id = employees.id")
  19.     .group("interaction_type", "employees.#{compare_key}")
  20.     .count
Advertisement
Add Comment
Please, Sign In to add comment