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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:14:44+00:00 2026-06-10T23:14:44+00:00

I have two models, User and TrainingSession: class User < ActiveRecord::Base has_many :training_sessions end

  • 0

I have two models, User and TrainingSession:

class User < ActiveRecord::Base
has_many :training_sessions
end

class TrainingSession < ActiveRecord::Base
belongs_to :user
end

Each User has a certain number of points based on the sum of the durations of their accumulated training_sessions (in TrainingSession model):

class TrainingSession
def points
  sum(:duration) / 6
end

Currently, after a user creates a new training_session, I am updating the points attribute in the User model as follows (from TrainingSession controller):

# POST /training_sessions
# POST /training_sessions.json
def create
@user = User.find_by_id(session[:user_id])
@training_session = @user.training_sessions.new(params[:training_session])
@points = @user.training_sessions.points
@user.update_attribute(:points, @points)
...

The problem is that each time a training_session is created, it doesn’t include the points that were just accrued by the user from the most recently created training_session. For example, if a user has 160 points, then they go for a 60 minute run, they will have gained 10 points to reach 170 points. But after creating that 60 minute run training_session, the User model updates it to 160 points. So it is always one step behind, so to speak. I tried using an after_save callback on the TrainingSession model, like so:

class TrainingSession < ActiveRecord::Base
after_save :update_points  

private

def update_points
 count = @user.training_sessions.sum(:duration)
 if count >= 1
  points = count / 6
 end
 @user.update_attribute(:points, points)
end

but then I get this error when trying to create a new training_session:

undefined method `training_sessions' for nil:NilClass

So, my question is, how do I keep up to date the :points attribute on my user model with the inclusion of the most recently created training_session?

  • 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-10T23:14:46+00:00Added an answer on June 10, 2026 at 11:14 pm

    @user is undefined in your model. That’s why you get the error.

    A training session belongs to a user, so in your model you can get the user via self.user or simply user. This code should work:

    def update_points
      count = self.user.training_sessions.sum(:duration)
      points = count / 6 if count >= 1
      self.user.update_attribute(:points, points)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
I have two models: User and Store class Store < ActiveRecord::Base belongs_to :user class
I have two models, each with their own Carrierwave uploaders: class User < ActiveRecord::Base
I have two models class User < ActiveRecord::Base has_many :requests, :foreign_key => :recipient_id .
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
Hey, need a little help here. I have two models: class User < ActiveRecord::Base

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.