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 8991341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:42:09+00:00 2026-06-15T22:42:09+00:00

Is there a way to use a model´s method in my scope or in

  • 0

Is there a way to use a model´s method in my scope or in my class method?

I have these two methods on my contact class, defining if a contact is active

def active
 return no_of_activities_last_year > 0 ? true : false
end


def no_of_activities_last_year
  time_range = (Time.now - 1.year)..Time.now
  activities.where(:updated_at => time_range).count
end

Then I would like to be able to return all active contacts by either scope or a class method, something like this:

scope :active_contacts, lambda {where(:active => true)} 

or

def self.active_contacts
 where(:active => true)
end

But they don´t work because the active is not part of the database.

Is there some clever way I can come by this? And preferable in a way that could be combined with other scopes?

  • 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-15T22:42:10+00:00Added an answer on June 15, 2026 at 10:42 pm

    As long as I know, there is not. The whole idea behind ActiveRecord is to make queries to the database. You cannot tell the database “get me all the records for which this ruby code logic is true”, however you can tell the database “get me all the records under this criteria”.

    So you can still use a inner select for that, there may be better ways but you can do:

    raw_data = User.connection.execute("select user_id from (SELECT count(*) as qty, user_id FROM activities f2 WHERE user_id is not null AND created_at > DATE_SUB(NOW(), INTERVAL 1 WEEK) GROUP BY user_id HAVING qty > 1) as f1")
    User.where("id in (?)", raw_data.to_a.flatten)
    

    Its not the cleanest solution, but its fast and it will give you an idea on how to build the other queries.

    If you don’t want to write raw sql and you want to do it the right way, you can always learn about Arel or use Squeel.

    Alternatively, you can fetch all the data and then filter by ruby code, but that will be inefficient compared to letting the sql engine to filter the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use the SQL Model (dbml) builder in VS2010 using
Is there any way to use an Entity as a model, and be able
Is there a way to use abstract or annotated class as MXBean descriptor? I
I have a repository class that defines some basic Get/Save/Delete methods. Inside these, I
Is there any way to use Google's API to retrieve a user's current zipcode
Is there a way to use the win32clipboard module to store a reference to
Is there a way to use lapply() in a manner that forces it to
Is there a way to use a different css for Android than is used
Is there some way to use Tuning Advisor for SQL Express? Is there some
Is there a way to use numpy from a local directory without adding the

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.