Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:22:20+00:00 2026-06-11T18:22:20+00:00

I’m rewriting a manual SQL query into an ActiveRecord query for use in a

  • 0

I’m rewriting a manual SQL query into an ActiveRecord query for use in a Rails app.

What it does it collect two groups of users from the same table: 1 who have a qualifying event (44 or 48) in the last week, and a group of users who have the same qualifying event a month ago.

This is the current query. I’m not sure how to turn this into an ActiveRecord scope:

select top 500 active_users_last_week.email 
    from (
      select distinct(email) from user_event_tracking
      where event_id in (44,48)
      and sitedb_created_date between getdate()-8 and getdate()-1
      and sitedb_name = 'XYZ'
      and email not like '%@company.com'
      and email not like '%@other_company.com'
    ) active_users_last_week, 
    (
      select distinct(email) from user_event_tracking
      where event_id in (44,48)
      and sitedb_created_date between getdate()-60 and getdate()-30
      and sitedb_name = 'XYZ'
      and email not like '%@company.com'
      and email not like '%@other_company.com
    ) active_users_last_month
where active_users_last_week.email = active_users_last_month.email;

Any suggestions on how to turn this into an ActiveRecord scope? I have these set as scopes already:

scope :active_events, lambda {
    where("event_id in (44,48)")
}

scope :for_property, lambda { |property| 
    where('sitedb_name = ?', property)
}

scope :last_week, lambda {
    where("sitedb_created_date between GETDATE()-8 and GETDATE()-1")
}

scope :last_month, lambda {
    where("sitedb_created_date between GETDATE()-60 and GETDATE()-30")
}

scope :no_test_users, lambda {
    where("email not like '%@company.com' and email not like '%@other_company.com'")
}

The scopes all work individually (and with each other). The question is how to get emails that are in both Event.active_events.last_week and Event.active_events.last_month in an efficient way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:22:21+00:00Added an answer on June 11, 2026 at 6:22 pm

    Try this:

    Event.select(:email).where(:event_id => [44,48], sitedb_created_date => (8.days.ago..1.day.ago), :sitedb_name => 'XYZ', :email => Event.select(:email).where(:event_id => [44,48], sitedb_created_date => (60.days.ago..30.days.ago), :sitedb_name => 'XYZ').where(Event.arel_table[:email].does_not_match_all(["%company.com","%other_company.com"])))
    

    You might need to tinker with days to adjust them to your date range, I am not 100% sure if they are inclusive or not. You might need to change 8.days.ago to 7.days.ago etc.

    You also should be able to do this with your scopes:

    Event.active_events.last_week.where(:email => Event.active_events.last_month.select(:email))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.