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

  • Home
  • SEARCH
  • 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 527725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:54:48+00:00 2026-05-13T08:54:48+00:00

I’m writing a Rails app in which I have two models: a Machine model

  • 0

I’m writing a Rails app in which I have two models: a Machine model and a MachineUpdate model. The Machine model has many MachineUpdates. The MachineUpdate has a date/time field. I’m trying to retrieve all Machine records that have the following criteria:

  1. The Machine model has not had a MachineUpdate within the last 2 weeks, OR
  2. The Machine model has never had any updates.

Currently, I’m accomplishing #1 with a named scope:

named_scope :needs_updates,
            :include => :machine_updates,
            :conditions => ['machine_updates.date < ?', UPDATE_THRESHOLD.days.ago]

However, this only gets Machine models that have had at least one update. I also wanted to retrieve Machine models that have had no updates. How can I modify needs_updates so the items it returns fulfills that criteria as well?

  • 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-05-13T08:54:48+00:00Added an answer on May 13, 2026 at 8:54 am

    One solution is to introduce a counter_cache:

    # add a machine_updates_count integer database column (with default 0)
    # and add this to your Machine model:
    counter_cache :machine_updates_count
    

    and then add OR machine_updates_count = 0 to your SQL conditions.

    However, you can also solve the problem without a counter cache by using a LEFT JOIN:

    named_scope :needs_updates,
      :select => "machines.*, MAX(machine_updates.date) as last_update",
      :joins  => "LEFT JOIN machine_updates ON machine_updates.machine_id = machines.id",
      :group  => "machines.id",
      :having => ["last_update IS NULL OR last_update < ?", lambda{ UPDATE_THRESHOLD.seconds.ago }]
    

    The left join is necessary so that you are sure you are looking at the most recent MachineUpdate (the one with MAX date).

    Note also that you have to put your condition in a lambda so it is evaluated every time the query is run. Otherwise it will be evaluated only once (when your model is loaded on application boot-up), and you will not be able to find Machines that have come to need updates since your app started.

    UPDATE:

    This solution works in MySQL and SQLite, but not PostgreSQL. Postgres does not allow naming of columns in the SELECT clause that are not used in the GROUP BY clause (see discussion). I’m very unfamiliar with PostgreSQL, but I did get this to work as expected:

    named_scope :needs_updates, lambda{
      cols = Machine.column_names.collect{ |c| "\"machines\".\"#{c}\"" }.join(",")
      {
        :select => cols,
        :group  => cols,
        :joins  => 'LEFT JOIN "machine_updates" ON "machine_updates"."machine_id" = "machines"."id"',
        :having => ['MAX("machine_updates"."date") IS NULL OR MAX("machine_updates"."date") < ?', UPDATE_THRESHOLD.days.ago]
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.