friendly_id belongs_to 型号

friendly_id on belongs_to model

我有一个设计 User 模型和一个配置文件模型 belongs_to User

class Profile < ApplicationRecord
    belongs_to :user
end

User 模型添加了一个名为 name 的列,用于存储用户名。

我正在使用 friendly_id gem 并想像这样将它应用到 Profile 模型:

class Profile < ActiveRecord::Base
  belongs_to :user
  extend FriendlyId
  friendly_id :name, use: :slugged
end 

但我不想在 Profile 模型中创建另一个名为 name 的列,我想在这里使用 User 模型列 name,配置文件 belongs_to.

来自 friendly_id 存储库的答案 owner to a similar question from Github issue:

Sorry, not going to do this. This was how FriendlyId 3.x and below worked. If you have very large tables, performance is significantly worse with the slug in a separate table.

所以简而言之,子弹必须放在同一个 table 中。