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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:46:59+00:00 2026-06-02T07:46:59+00:00

I have users that have many points and points belongs to users. Points can

  • 0

I have users that have many points and points belongs to users.

Points can be any amount.

I want to be kind to my db by adding a total points cache to my user model so I can easily just do user.total_points

how do I go about implementing this?

EDIT: PS I am adding points for the first time so there’s no need to go back and calculate points that users may have already earned…just need it going forward

  • 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-02T07:47:01+00:00Added an answer on June 2, 2026 at 7:47 am

    You can use the counter_cache technique if row in the points table constitutes one point. Otherwise you can implement it using a simple after_save filter.

    class User
      # add a column called total_points and set the default to 0    
      has_many :points    
    end
    
    
    class Point
    
      # assuming `amount` column stores the points.    
      belongs_to :user
    
      after_create { |p| p.update_user_points(:create) }
      after_update { |p| p.update_user_points(:update) if p.amount_changed? }
      after_destroy{ |p| p.update_user_points(:destroy)}
    
      def update_user_points(type)
        delta = (type == :create) ? amount :
                   (type == :update) ? (amount - amount_was) :
                     -amount
        User.update_counters(user, :total_points => delta) 
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-one relationship that users can edit via checkboxes. PK of Foo
I have a user model and point model. A user can have many points
I've an application in the market and many users have reported that the app
Given models User and Comments being populated such that a user may have many
I have two tables of concern here: users and race_weeks. User has many race_weeks,
If User s have points, how can I get the user rank, assuming standard
I have users in my system that can elect to 'hibernate', at which point
I have an Organization model that has_many users through affiliations. And, in the form
I have a model called company that has_many users then users belongs_to company. class
I have two tables: sites has_many users users belongs_to sites Is it better that

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.