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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:26:20+00:00 2026-05-30T02:26:20+00:00

I’m using Rails 3.1, MySQL and working with 3 classes: Computer, LifecycleStatus & LifecycleEntry

  • 0

I’m using Rails 3.1, MySQL and working with 3 classes: Computer, LifecycleStatus & LifecycleEntry

I want to track the history of a Computer’s LifecycleStatus over time so the columns in LifecycleEntry are: computer_id:integer, lifecycle_status_id:integer and changed_at:datetime. A computer can have multiple entries in LifecycleStatus.

I have the following associations set up:

Computer
  has_many :lifecycle_entries, :order => "changed_at DESC"
  has_many :lifecycle_statuses, :through => :lifecycle_entries

LifecycleEntry
  belongs_to :computer
  belongs_to :lifecycle_status

LifecycleStatus
  has_many :lifecycle_entries

I’d like to see, for a particular LifecycleStatus, which computers are currently (their most recent lifecycle_entries record) assigned to that status.

I’ve managed to create the correct SQL to retrieve this info, but am not sure how to translate this to a Rails association:

SELECT id, le.computer_id, lifecycle_status_id
FROM lifecycle_entries AS le
INNER JOIN (
    SELECT lemax.computer_id, MAX(changed_at) AS latest
    FROM lifecycle_entries AS lemax
    GROUP BY lemax.computer_id
) maxdates
ON le.changed_at = maxdates.latest
AND le.computer_id = maxdates.computer_id
WHERE lifecycle_status_id = 6
  • 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-30T02:26:22+00:00Added an answer on May 30, 2026 at 2:26 am

    It seems like this would be simpler if you denormalized the ‘active’ status onto the LifecycleEntry itself, updating it each time a new entry is created. This saves you a GROUP BY / MAX query each time you read.

    If you have the status column it would be as simple as LifecycleEntry.active.where(:lifecycle_status_id => 6).computers, where active is a scope on LifecycleEntry.

    So do the following:

    • Add an active boolean field to LifecycleEntry
    • Add a callback to the LifecycleEntry model to set the active entry

    Here is the callback:

    after_save :set_active
    
    private
    
    def set_active
      last_active_entry = self.computer.active_lifecycle_entry
    
      if last_active_entry.nil?
         update_attributes(:active => true)
      elsif changed_at > last_active_entry.changed_at
         last_active_entry.update_attributes(:active => false)
         update_attributes(:active => true)
      end
    end
    

    It’s important to note that the LifecycleEntry being created may precede the currently active one, so you must only set the new one to active if the changed_at is after. Also, for the first LifecycleEntry created, active is always set.

    • 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'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

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.