validate_uniqueness_of scope not working

441 views Asked by At

Currently I can create one item with multiple designs in unique picturelocs. Creating a second item gives me the following error that there already exists a pictureloc with that value, even though it is in a seperate item_id and shouldn't be worried over. This is my first question so cut me some slack on the formatting, and thanks in advance!

Error:

PG::Error: ERROR:  duplicate key value violates unique constraint "index_designs_on_pictureloc"
DETAIL:  Key (pictureloc)=(1) already exists.
: INSERT INTO "designs" ("created_at", "item_id", "picture_content_type", "picture_file_name", "picture_file_size", "picture_updated_at", "pictureloc", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"

Design.rb

class Design < ActiveRecord::Base
  attr_accessible :picture, :pictureloc

  has_attached_file :picture, styles: {medium: "150x150#"}
  validates_attachment :picture, presence: true,
          content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png'], :message         => 'must be a PNG, JPG, or JPEG'},
      size: {less_than: 5.megabytes, :message => 'must be less than 5 megabytes'}
  validates_uniqueness_of :pictureloc, scope: :item_id

  belongs_to :item

  def show_location
    if pictureloc == 1
        "Front"
    elsif pictureloc == 2
        "Back"
    elsif pictureloc == 3
        "Left Sleeve"
    elsif pictureloc == 4
        "Right Sleeve"
    end
  end
end

Item.rb

class Item < ActiveRecord::Base
  attr_accessible :price, :make, :amount, :color, :note, :designs_attributes
  validates_presence_of :make, :amount, :color
  validates :amount, :numericality => { :greater_than => 0 }

  belongs_to :quote
  has_many :designs, :dependent => :destroy
  accepts_nested_attributes_for :designs
end

Index in the schema:

 add_index "designs", ["pictureloc"], :name => "index_designs_on_pictureloc", :unique => true

nested item view:

<!-- item form -->
<%= f.input :make, collection: @types, label: 'Thread Type' %>
<%= f.input :amount, label: 'How Many' %>
<%= f.input :color, collection: @colors %>
<!-- nested form for creating a design of an item -->
<%= f.simple_fields_for :designs, :html => { :multipart => true } do |designform| %>
    <%= render "customdesign", g: designform %>
  <% end %>
<!-- add/remove another design -->  
<%= f.link_to_add "Add Design", :designs %>
<%= f.input :note, :input_html => { :cols => 50, :rows => 3 }, label: 'Special Notes or Requests' %>
<%= f.link_to_remove "Remove" %>

Nested design view:

<!-- upload/remove image form -->
<%= g.select( :pictureloc, { "Front" => 1, "Back" => 2, "Left Sleeve" => 3, "Right Sleeve" => 4 } ) %>
<%= g.file_field :picture %>
<%= g.link_to_remove "Remove" %>

Note that pictureloc is an integer, and that the designs get saved at the same time which is why I had to create an index (I think?)

I tried removing the unique: true from the index because maybe it was overkill, but that didn't solve anything.

1

There are 1 answers

0
BvuRVKyUVlViVIc7 On BEST ANSWER

Your index should be

add_index "designs", ["pictureloc", "item_id"], :name => "index_designs_on_pictureloc", :unique => tru