All threads / Displaying previous score in current record
Ask A Question


You’re not receiving notifications from this thread.

Displaying previous score in current record

John Munyi asked in General

Hi Chris I got a lil logic issue going on here, have a look at this gist

so technically what i want is for each new weekly_performance_review the target(has 3 fields) is equal to the last weeks next_target(has 3 fields also)


I have also factoting in this in the def new

    def new
       @weekly_performance_review =
      @users = { |u| [,] }

but it isnt helping much

Any fresh ideas

Interesting. Let's see if I'm understanding this right, are you looking to compare two WeeklyPerformanceReview objects?

Hi Chris,

Yes sought of .... so this is the use case. when we create a weekly_performance_review (this is done weekly) we have a target score which was set in the prev weeks review ... which is what we want to display during this weeks review on the new view, so that when we enter the actual score we can compare what we set last week vs what you have attained .

Oh I see. That makes sense.

So you probably need to be able to find the previous weekly performance review from any record right? Are you setting week_start to like the Sunday or Monday beginning of each week?

i tried using a class method:

   def last_score

But the previous score kept getting overwritten across all records

I am using

   def week_of
         d =
         "Starting:"" ""#{d.beginning_of_week}"" ""Ending:"" ""#{d.beginning_of_week + 4}"

to set a week ... whihc is covering Monday thro friday

I was going to suggest something similar.

def last_score
  user.weekly_performance_reviews.order(week_start: :desc).last

This would do sorting based upon the week start (assuming that was a date) but your suggestion there would work as well since it's ordering by the IDs as long as those count up.

You might have something else going wrong then if records are getting overwritten. I'm not too sure about that. Any other info you have as to what might be going wrong?

Oh wait, I think that's it! You're actually calculating the the beginning of week from today every single time you access it. Instead of using, I think you probably want to use the column saved to your record, probably week_start. That should fix it!

Let me try that right away

I actually don’t have a column for that day .. since its just for displaying purposes, but let me give it a shot

Interesting, then you may either need to calculate it based on some information you have stored, or you may need to start storing a day for that week. Either way, you will want some want to store what week that represented which will allow you to display the correct date if that makes sense.

with this :

   def last_score
       self.user.weekly_performance_reviews.order(week_start: :desc).last.kpi_quality_next_target

we seen constrained to only 1 week, yet we want to compare the current weeks actual score vs the target that was set in the previous week , correct ?

This would be if you had the previous record for last week that contained that information, yes. You'd access this from the current week's record, and then you could compare the numbers between them basically.

we actually do have a previous record ... but it seems to only display the target set within this week only . We want to access the previous weeks record indeed

I think its the date , had totally missed your earlier comments

That's what I'm thinking too. Not sure I'll be able to help too much other than that, but let me know if you figure it out!

Hi Chris,

So i finally got a work around to this issue of overwriting the last value on every-record, i wrote a custom method to track the id of the current record and then returns the prev one, this is called directly on the view now. Here it is

      def prev
          self.user.weekly_performance_reviews.where("id < ?", id).last

with the above now, on the view i can call

                     <%= @weekly_performance_review.prev.kpi_quality_next_target %>

which returns the score of the specific previous record , any other efficient way we can modify this ? so far it works though

Awesome work John! :) I've done that exact same thing in a few apps and it works nicely. That's going to be the most efficient way to do that because you will not know which records have been deleted (or whatever), so you'll always need to do the query to safely retrieve the associated record.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 70,078+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.