Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top