比较两个用户

时间:2015-01-16 20:14:45

标签: ruby-on-rails ruby ruby-on-rails-4 associations similarity

我想根据教育,经验,技能和国家/地区将用户个人资料与其他用户个人资料进行比较。如果教育相似,则得分为20,否则为0. total_score将是得分的总和。然后,我可以根据总分数对类似于给定用户的配置文件进行排序。任何人都可以帮我解决这个问题吗?

我不知道如何比较同一个表中的两个用户或两件事,我不知道将代码放在哪里:配置文件模型,帮助程序或什么。

1 个答案:

答案 0 :(得分:0)

将此添加到您的用户模型。

def total_score
  education+skills+country #modify to determine score, idk how you plan to determine this
end

尝试排序时:

User.all.sort_by{|a| a.total_score} #returns a list of users sorted by total score.

修改

我认为你的意思是:

def total_score
  score=0
  if education=="First School"
     score+=10
  elsif education=="Second School"
     score+=5
  end

  if skill=="Skill 1"
     score+=10
  elsif education=="Skill 2"
     score+=5
  end

  if country=="Country 1"
     score+=10
  elsif country=="Country 2"
     score+=5
  end
end