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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:09:37+00:00 2026-06-09T15:09:37+00:00

I want to return results to a user sorted by a scoring algorithm that

  • 0

I want to return results to a user sorted by a scoring algorithm that is personalized to the user based on their friends. For context, in my app, users have events that they can add to their calendar. Myevents is a join table between users and events. I want to calculate a score based on how many total attendees there are to an event and how many of a user’s friends are attending that event.

I want to do something like this:

class Event < ActiveRecord::Base

after_initialize :calculate_score
attr_accessor :score



  def calculate_score(user_id)


     User.find_by_id(user_id).friends.each do |friend|

                        if @id = Myevents.find_by_user_id_and_flier_id(friend.id, self.id)

                            friends_attending_list << @id
                        end

      end

    friends_attending_count = friends_attending_list.count

    total_attendees_count = Myevents.where(:flier_id => self.id, :attending_status => "1").count

     self.score = 100*(friend_attending_count) + 50*(total_attendees_count)

  end


end

Unfortunately, I can’t find a good way to pass the session’s user_id to the model….ideally, I would like to not violate MVC but I want to be able to sort results by this calculated score. Any advice?

  • 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-09T15:09:38+00:00Added an answer on June 9, 2026 at 3:09 pm

    First, to scale, you would need to cache this score somewhere. This method here would be veryyy expensive. But it’s not a simple problem since it’s a score per user per event.

    I dont see how it makes sense to store the score on the event.

    Without thinking of performances, I would have a score_for_event(event) method on the User model that you can call wherever you need.

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

Sidebar

Related Questions

I am having trouble pulling a query where i want to return results that
I want to write a mysql statement which will return a list of results
I have a query that I always want to return 10 records for: set
I want to be able to search and return results across the majority of
Let´s say I a have an User class and I want to return all
I have a query that joins 3 tables and returns the results sorted by
I am trying to return results based on the number of cases if greater
I want to return a simple json result, something like: { 12323: true }
I have a MYSQL stored procedure SP1() that returns a result set. I want
I want to return a specific message on error of an ajax request. [WebMethod]

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.