我想将结果返回给用户,该用户按照基于朋友对用户个性化的评分算法进行排序。对于上下文,在我的应用中,用户可以将事件添加到他们的日历中。 Myevents是用户和事件之间的连接表。我想根据一个活动的参与者总数以及有多少用户的朋友参加该活动来计算得分。
我想做这样的事情:
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
不幸的是,我无法找到将会话的user_id传递给模型的好方法....理想情况下,我希望不违反MVC,但我希望能够按以下方式对结果进行排序这个计算得分。有什么建议吗?
答案 0 :(得分:3)
首先,要进行扩展,您需要在某处缓存此分数。这种方法非常昂贵。但这不是一个简单的问题,因为它是每个用户每个事件的得分。
我没看到将比分存储在比赛中是多么合理。
如果不考虑表演,我会在用户模型上使用score_for_event(event)
方法,您可以在任何需要的地方拨打电话。