0

I am trying to convert this query into the active record but don't get any luck. Can anyone guide me how to convert this query from raw SQL to active record query

select users.* from
    (
    select email from players_schema.inv_emails where inv_emails.created_at <= (current_date - interval '14 days')
    union
    select email from players_schema.blks where blks.created_at <= (current_date - interval '14 days')
    union
    select email from players_schema.bouces where bouces.created_at <= (current_date - interval '14 days')
    ) email
    join football_schema.users on users.email = email.email; 

I am using rails 4.2

Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
  • 1
    Please check this https://stackoverflow.com/questions/6686920/activerecord-query-union it may help you. – srinij Jun 11 '18 at 06:38

1 Answers1

0

Finally, I got the solution

User.from("(#{players_schema.inv_emails.to_sql} UNION # 
{players_schema.bouces.to_sql} UNION #{players_schema.blks.to_sql} ) 
AS invalid_emails").joins("INNER join football_schema.users on 
users.email = invalid_emails.email")
Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61