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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:41:05+00:00 2026-06-09T11:41:05+00:00

I’ve got 2 tables in my DB. users and trackers . Each Tracker has

  • 0

I’ve got 2 tables in my DB. users and trackers.

Each Tracker has a user_id relating to the users table as you’d expect.

The context of this app is that it’s part of a team based time tracking solution. As part of this I need to sort users by the total amount of time they’ve tracked to date and also display this total on screen.

So I figured that whenever a Tracker is saved I fire a before_save callback that grabs the start_time and end_time values from the Tracker object and does a simple (end_time - start_time).to_i to get the time of the tracker in seconds. The outcome is saved to Tracker.tracked_time. This part works and I can see the calculated value in the DB.

I now need to save a total result of all Tracker instances’ tracked_time value per user and then I’d like to store this value to the users table in the tracked_time column there so that I can pull this out from the database to sort results by (think: most active user, least active user, top 10).

However when I try calling this before_save :calculate_tracked_time, if: :finished?, unless: :admin? I simply get a ActiveRecord::RecordNotSaved error.

It appears that I cannot save a value on a User model while inside the Tracker module… but I have no clue as to why or how else to do what I need.

Any thoughts on how to make this work (or work better perhaps) would be much appreciated.

Oh and the :tracked_time on the User model is listed in the attr_accessible so it can be written to.

Below is the Tracker model, I can also post the User model if needed.

class Tracker < ActiveRecord::Base
  attr_accessible :running, :start_time, :end_time, :tracked_time, :intention, :achievement
  belongs_to :user

  before_save :calculate_tracked_time, if: :finished?, unless: :admin?

  validates :user_id,   presence: true
  validates :intention, presence: true,
                        length: { minimum: 5 }

  default_scope order: 'trackers.created_at DESC'

  private

    def finished?
      # Only save the total amount of time elapsed if we have
      # a start_time AND end_time for this object.
      !self.end_time.nil?
    end

    def admin?
      # Don't calculate totals for the admin user.
      User.find_by_id( self.user_id ).admin?
    end

    def calculate_tracked_time

      # write the tracker's tracked time value in seconds
      self.tracked_time = ( self.end_time - self.start_time ).to_i

      # update the user's tracked time total
      user = User.find_by_id( self.user_id )
      # calculate time from all trackers this user has
      total_time = 0
      user.trackers.each { |t| total_time += t.tracked_time }
      # this log correctly prints the correct figures.
      p "Total Time for user (#{self.user_id}): #{total_time}"
      # store it to the user's :tracked_time db column
      user.tracked_time = total_time
      # save !!! Errors out.
      user.save
    end

end

Thanks for reading.

  • 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-09T11:41:06+00:00Added an answer on June 9, 2026 at 11:41 am

    Instead of updating the field and calling save, try using update_column:

    user.update_column(:tracked_time, total_time)
    

    update_column doesn’t call any callbacks (or validations, so you do need to be careful). Callbacks can prevent saving when you’re already in a callback.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have two tables with like below codes: Table: Accounts id | username |
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.