如何在用户和所有其他用户之间建立关系?

时间:2017-04-29 12:48:20

标签: ruby-on-rails rails-activerecord matchmaking

我正在创建我的第一个Rails应用程序来学习基础知识,它是一个简单的配对网站。

我试图设计用户彼此关联的方式。匹配百分比将通过比较某些用户配置文件属性来确定。每个用户将拥有每个其他用户的匹配百分比(类似于OKCupid)。因此,当创建(或更新)用户时,将与其一起生成匹配。

这是我对它到目前为止的看法的想法:

# 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

我不确定的部分是如何设置用户模型,并将外键传递给matchable。这样做的正确方法是什么?欢迎任何建议/意见。

0 个答案:

没有答案