0

I'm creating my first Rails app to learn the basics, and it's a simple matchmaking site.

I'm trying to design the way the users will associate with each other. The match percentage will be determined by comparing certain user profile attributes. Every user will have a match percentage for every other user (similar to OKCupid). So when a user is created (or updated), it's matches are generated along with it.

This is my idea of how it'll look so far:

# scheme.rb
create_table "matchables", force: :cascade do |t|
    t.integer  "user_one_id"
    t.integer  "user_two_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.index ["user_one_id"], name: "index_matchables_on_user_one_id"
    t.index ["user_two_id"], name: "index_matchables_on_user_two_id"
    t.index ["user_one_id", "user_two_id"], 
            name: "index_matchables_on_user_one_id_and_user_two_id", 
            unique: true
end

# matchable.rb
class Matchable < ApplicationRecord
   belongs_to :user_one, class_name: "User"
   belongs_to :user_two, class_name: "User"
   validates :user_one_id, presence: true
   validates :user_two_id, presence: true

   def percent
     # match percent method will go here
   end
end

# user.rb
class User < ApplicationRecord
  has_many :matchables, 
            class_name: "Matchable",
            dependent: :destroy
  has_many :matches, through: :matchable, source: :match
end

The part I'm unsure about is how the user model should be set up with the foreign key being passed to matchable. What is the proper way to do this? Any suggestions / advice welcome.

helpmeplz
  • 151
  • 1
  • 3
  • 11
  • This is actually quite a tough pickle to solve since the user can be in either `user_one_id` or `user_two_id` - http://stackoverflow.com/questions/37244283/how-to-model-a-mutual-friendship-in-rails – max Apr 29 '17 at 23:20
  • I think you're right. I'm not sure how to approach it. – helpmeplz Apr 30 '17 at 05:13

0 Answers0