title: Validate scoped uniqueness in polymorphic association models
body: <p>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. </p>
  
  <pre><code>class Favorite &lt; ActiveRecord::Base
   belongs_to :favoritable, :polymorphic =&gt; true
   belongs_to :user
  
   attr_accessible :user
  
   validates_presence_of :user
   validates :user_id, :uniqueness =&gt; { :scope =&gt; [:favoritable_type, :favoritable_id] }
  end
  </code></pre>
  
  <p>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.</p>
  
  <p><img src="https://i.stack.imgur.com/mA4DM.png" alt="enter image description here"></p>
  
  <p>Is there a way to stop this initial save?</p>
  
  <p>It seems that Rails is creating the DB entry and then updating it with the favoritable_id and favoritable_type as follows:</p>
  
  <pre><code>  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
  </code></pre>