简体   繁体   中英

How to convert this query into active record

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM