mardi 12 mai 2015

Finding similar values in a hash/array in Ruby

I am trying to compute how many 'tips' a 'user' has in common with another user. Here is my method doing so:

class User < ActiveRecord::Base
  has_many :tips, :inverse_of => :user
  ...
  public
  def tip_affinity_with(user)
    tipAffinity = 0

    user.tips.each do |tip|
      if self.tips.include?(tip)
        tipAffinity = tipAffinity + 1
      end
    end

    return tipAffinity
  end
end

I know that some users have tips that they have both rated, but in my table, the tipAffinity is 0 for all of the users.

What could be the problem? Any help is greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire