0

我正在创建我的第一个 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

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

4

0 回答 0