对,所以我有一个多态关联,允许收藏不同的对象类型。所以一个人可以喜欢一个产品,一个人,或者其他什么。我想要做的是防止有人使用验证收藏模型中的唯一性来复制收藏。
class Favorite < ActiveRecord::Base
belongs_to :favoritable, :polymorphic => true
belongs_to :user
attr_accessible :user
validates_presence_of :user
validates :user_id, :uniqueness => { :scope => [:favoritable_type, :favoritable_id] }
end
验证似乎正在工作,但无论出于何种原因,当尝试复制条目时,仍会使用 user_id 创建一个新的收藏行。
有没有办法停止这个初始保存?
似乎 Rails 正在创建数据库条目,然后使用 favouritable_id 和 favouritable_type 更新它,如下所示:
SQL (28.3ms) INSERT INTO "favorites" ("created_at", "favoritable_id", "favoritable_type", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Tue, 14 Aug 2012 10:26:31 UTC +00:00], ["favoritable_id", nil], ["favoritable_type", nil], ["updated_at", Tue, 14 Aug 2012 10:26:31 UTC +00:00], ["user_id", 23]]
(7.8ms) COMMIT
(0.1ms) BEGIN
Favorite Exists (0.3ms) SELECT 1 AS one FROM "favorites" WHERE ("favorites"."user_id" = 23 AND "favorites"."id" != 123 AND "favorites"."favoritable_type" = 'Style' AND "favorites"."favoritable_id" = 29) LIMIT 1
(0.2ms) UPDATE "favorites" SET "favoritable_id" = 29, "favoritable_type" = 'Style', "updated_at" = '2012-08-14 10:26:31.943937' WHERE "favorites"."id" = 123
(6.7ms) COMMIT
(0.1ms) BEGIN