views:

70

answers:

2

I have an algorithm that searches through all of my sites users, finding those which share a common property with the user using the algorithm (by going to a certain page). It can find multiple users, each can have multiple shared properties. The algorithm works fine, in terms of finding the matches, but I'm having trouble working out how to store the data so that later I'll be able to use each unit of information. I need to be able to access both the found users, and each of the respective shared properties, so I can't just build a string. This is an example of the output, being run from the perspective of user 1:

user 4
sharedproperty3
sharedproperty6

user 6
sharedproperty6
sharedproperty10
shareproperty11

What do I need to do to be able to store this data, and have access to any bit of it for further manipulation? I was thinking of a hash of a hash, but I can't really wrap my head around it. I'm pretty new to programming, and Ruby in particular. Thanks for reading!

EDIT - Here's the code. I'm fully expecting this to be the most incorrect way to do this, but it's my first try so be gentle :) So if I'm understanding you guys correctly, instead of adding the interests to a string, I should be creating an array or a hash, adding each interest as I find it, then storing each of these in an array or hash? Thanks so much for the help.

def getMatchedUsers
  matched_user_html = nil
  combined_properties = nil
  online_user_list = User.logged_in.all
    shared_interest = false
    online_user_list.each do |n| # for every online user
      combined_properties = nil
      if n.email != current_user.email # that is not the current user 
      current_user.properties.each do |o| # go through all of the current users properties
        n.properties.each do |p| # go through the online users properties
              if p.interestname.eql?(o.interestname) # if the online users property matches the current user
                  shared_interest = true
                  if combined_properties == nil
                    combined_properties = o.interestname
                  else
                    combined_properties = combined_properties + ", " + o.interestname
                  end
              end
          end
          if shared_interest == true
                matched_user_html = n.actualname + ": " + combined_properties
            end
      end
    end
    end
    return matched_user_html
  render :nothing => true
end
+1  A: 

This returns an array of hashes with all users and their corresponding sharedproperties.

class User
  def find_matching_users
    returning Array.new do |matching_users|
      self.logged_in.each do |other_user|
        next if current_user == other_user # jump if current_user
        # see http://ruby-doc.org/core/classes/Array.html#M002212 for more details on the & opreator
        unless (common_properties = current_user.properties & other_user.properties).empty?
          matching_users << { :user => other_user, :common_properties => common_properties }
        end
      end
    end
  end
end

In your view you can do something like this:

<%- current_user.find_matching_users.each do |matching_user| -%>
  <%-# you can acccess the user with matching_user[:user] -%>
  <%-# you can acccess the common properties with matching_user[:common_properties] -%>
<%- end -%>
jigfox
Wow, thanks so much for this! Time to read up
ben
+1  A: 

You can use a hash table with the key being the user object and the value being an array of the shared properties . This is assuming that you first need to do a lookup based on the user .

Something like this :

 @user_results = { user1 => [sharedproperty3,sharedproperty7] , user2 => [sharedproperty10,sharedproperty11,sharedproperty12]}

You can then acces the values like : @user_results[user1]
or you can also iterate over all the keys using @user_results.keys

NM
Would I access a sharedproperty like this (@user_results[user1])[0]?
ben
@user_results[user1] return you an array . so basically @property_array = @user_results[user1] . And then you can access the shared properties like @property_array[0] , @property_array[1] etc .
NM
thanks for your help!
ben