Right, so I have a polymorphic association that allows for different object types to be favorited. So a person can favorite a product, or a person, or whatever. What I want to do is guard against someone duplicating a favorite using validates uniqueness in the Favorite model.
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
The validation seems to be working but for whatever reason a new Favorite row is still created with the user_id when an attempt is made to duplicate the entry.
Is there a way to stop this initial save?
It seems that Rails is creating the DB entry and then updating it with the favoritable_id and favoritable_type as follows:
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
If you closely observe you can find that uniqueness validation just work fine :)
validates :user_id, :uniqueness => { :scope => [:favoritable_type, :favoritable_id] }
Look at the data image you added. inside image you can find out that second record is not having favouritable
whereas first have which is different hence 2 records are uniq and its not issue with uniqueness
but its your logical gap.
If you strictly wanted to avoid second entry then keep favouritable
as mandatory field
validates :favoritable_type, :favoritable_id, :presence => true
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :favoritable, polymorphic: true
validates :user_id, :favoritable_id, presence: true,
numericality: { only_integer: true }
validates :favoritable_type, presence: true,
inclusion: {
in: %w(FirstModelName SecondModelName),
message: "%{value} is not a valid"
}
validates :user_id, uniqueness: { scope: [ :favoritable_type, :favoritable_id ] }
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With