避免在 rails 和friendly_id 中进行双重唯一检查

问题描述

我使用friendly_id 在我的rails 6 应用程序中生成slug。 我意识到每次添加记录都会检查唯一名称

TRANSACTION (0.1ms)  BEGIN
Car Exists? (0.1ms)  SELECT 1 AS one FROM "cars" WHERE "caRSS"."id" IS NOT NULL AND "cars"."slug" = $1 LIMIT $2  [["slug","audi-4"],["LIMIT",1]]
Car Exists? (0.2ms)  SELECT 1 AS one FROM "cars" WHERE "caRSS"."id" IS NOT NULL AND "cars"."slug" = $1 LIMIT $2  [["slug",1]]
Car Create (0.2ms)  INSERT INTO "cars" ("name","p","slug","created_at","updated_at","x") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "id"  [["name","Audi-4"],["p","1500099948CC"],["slug",["created_at","2021-02-16 13:22:16.298773"],["updated_at",["x","hhc3av7GPbRzBsYwRPFiPIkQhRA4R8G9"]]
TRANSACTION (0.8ms)  COMMIT

如果我删除

  def should_generate_new_friendly_id?
    name_changed?
  end

它只执行一次。

看起来检查也是在添加时完成的,而不是像在更新时那样。

如何防止名称的唯一性被检查两次?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)