akasuki101
akasuki101

Reputation: 51

SQLite3::ConstraintException: UNIQUE constraint failed: action_text_rich_texts.record_type, action_text_rich_texts.record_id

I cannot create new product,and get this error although product.valid? = true:

ActiveRecord::RecordNotUnique in Admin::ProductsController#create
SQLite3::ConstraintException: UNIQUE constraint failed: action_text_rich_texts.record_type, action_text_rich_texts.record_id, action_text_rich_texts.name

I don't know why. Here is my product's schema:

create_table "products", force: :cascade do |t|
    t.string "code"
    t.integer "real_price"
    t.integer "sale_price"
    t.integer "remaining_amount"
    t.string "title"
    t.text "description"
    t.text "detail"
    t.string "seo_title"
    t.string "seo_keyword"
    t.string "seo_description"
    t.integer "product_category_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["code"], name: "index_products_on_code", unique: true
    t.index ["product_category_id"], name: "index_products_on_product_category_id"
    t.index ["title"], name: "index_products_on_title", unique: true
  end

And here is my product model:

class Product < ApplicationRecord
  belongs_to :product_category
  has_many_attached :images
  has_rich_text :detail

  validates :code, :title, presence: true
  validates :code, :title, uniqueness: { case_sensitive: false }
end

And here is my admin/products_controller.rb

class Admin::ProductsController < AdminController
  ...   
  def create
    @product = Product.new(product_params)
 
    if @product.save
      redirect_to  
    else
      render 'new'
    end
  end

  private

  ...

  def product_params
    params.require(:product).permit(:code, :real_price, :sale_price, :remaining_amount,
                                    :title, :description, :detail, :seo_title, :seo_keyword, :seo_description,
                                    :product_category_id, images: [])
  end
end

Please help me fix this error!

Upvotes: 0

Views: 254

Answers (0)

Related Questions