Ask A Question

Notifications

You’re not receiving notifications from this thread.

Displaying previous score in current record

JM asked in General

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

   https://gist.github.com/bl4ckdu5t/ec48cc9d6eedeb877d06

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)

Appreciated

I have also factoting in this in the def new

    def new
       @weekly_performance_review = WeeklyPerformanceReview.new
      @users = User.all.map { |u| [u.name, u.id] }
  end

but it isnt helping much

Any fresh ideas

Reply

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

Reply

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 .

Reply

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?

Reply

i tried using a class method:

   def last_score
       self.user.weekly_performance_reviews.last.kpi_quality_next_target
   end

But the previous score kept getting overwritten across all records

Reply

I am using

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

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

Reply

I was going to suggest something similar.

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

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?

Reply

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 Date.today, I think you probably want to use the column saved to your record, probably week_start. That should fix it!

Reply

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

Reply

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.

Reply

with this :

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

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 ?

Reply

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.

Reply

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

Reply

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

Reply

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!

Reply

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
     end 

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

Reply

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.

Reply
Join the discussion
Create an account Log in

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

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

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