1

我有兩個模型 - CategoryProperty與has_balongs_to_many關聯。我使用nested_form寶石。所以,類別有很多屬性。當我創建新的類別時,我可以創建屬性。嵌套驗證唯一性在導軌4中不起作用項目

分類模型category.rb

class Category < ActiveRecord::Base 
    # relationships 
    has_many :categories_properties 
    has_many :properties, through: :categories_properties, inverse_of: :categories 

    # allows to create and destroy nested objects 
    accepts_nested_attributes_for :properties, allow_destroy: true 

    # validation 
    validates :title, presence: true, length: { minimum: 3, maximum: 128 }, uniqueness: true 
end 

房產模型property.rb

class Property < ActiveRecord::Base 
    # relationships 
    has_many :categories_properties 
    has_many :categories, through: :categories_properties 

    # validation 
    validates :title, presence: true, length: { minimum: 3, maximum: 128 }, uniqueness: true 
end 

正如你看到一個擁有屬性模型uniqueness: true驗證。 當我試圖使用rails consolecategory edit page創建相同的屬性 - 它給我錯誤,如「屬性與此名稱已存在。」這是正確的,它應該是。

但在category new頁面上,當我創建相同的屬性(如您在屏幕截圖中看到的)時,它不會給我帶來錯誤,驗證不起作用,它會爲我創建具有兩個相同屬性的新類別....怎麼了?請幫忙。

enter image description here

這裏的日誌:

Started POST "/categories" for 127.0.0.1 at 2014-09-01 14:28:19 +0300 
Processing by CategoriesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"AFf8upQco8ZqJBS8QdpU9RIRpvAW1VLnBSm1bw6rxss=", "category"=>{"title"=>"Category", "description"=>"category description", "order"=>"12345", "icon"=>"http://4.bp.blogspot.com/-LOX6N2kXXaY/T5UtocrGRnI/AAAAAAAAAFU/EW_OZTHT1PI/s1600/1210167310_174374.jpg", "parent_id"=>"", "properties_attributes"=>{"1409570848547"=>{"title"=>"same properties", "_destroy"=>"false"}, "1409570857024"=>{"title"=>"same properties", "_destroy"=>"false"}}}, "commit"=>"Create Category"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
    (0.4ms) SELECT "categories"."id", "categories"."title" FROM "categories" 
    CACHE (0.0ms) SELECT "categories"."id", "categories"."title" FROM "categories" 
    (0.2ms) BEGIN 
    Property Exists (0.4ms) SELECT 1 AS one FROM "properties" WHERE "properties"."title" = 'same properties' LIMIT 1 
    CACHE (0.0ms) SELECT 1 AS one FROM "properties" WHERE "properties"."title" = 'same properties' LIMIT 1 
    Category Exists (0.6ms) SELECT 1 AS one FROM "categories" WHERE "categories"."title" = 'Category' LIMIT 1 
    SQL (0.5ms) INSERT INTO "categories" ("created_at", "description", "icon", "order", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", "2014-09-01 11:28:19.908849"], ["description", "category description"], ["icon", "http://4.bp.blogspot.com/-LOX6N2kXXaY/T5UtocrGRnI/AAAAAAAAAFU/EW_OZTHT1PI/s1600/1210167310_174374.jpg"], ["order", 12345], ["title", "Category"], ["updated_at", "2014-09-01 11:28:19.908849"]] 
    SQL (0.3ms) INSERT INTO "properties" ("created_at", "title", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", "2014-09-01 11:28:19.910971"], ["title", "same properties"], ["updated_at", "2014-09-01 11:28:19.910971"]] 
    SQL (0.4ms) INSERT INTO "categories_properties" ("category_id", "created_at", "property_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["category_id", 77], ["created_at", "2014-09-01 11:28:19.921763"], ["property_id", 90], ["updated_at", "2014-09-01 11:28:19.921763"]] 
    SQL (0.4ms) INSERT INTO "properties" ("created_at", "title", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", "2014-09-01 11:28:19.947583"], ["title", "same properties"], ["updated_at", "2014-09-01 11:28:19.947583"]] 
    SQL (0.4ms) INSERT INTO "categories_properties" ("category_id", "created_at", "property_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["category_id", 77], ["created_at", "2014-09-01 11:28:19.950243"], ["property_id", 91], ["updated_at", "2014-09-01 11:28:19.950243"]] 
    (0.5ms) COMMIT 
Redirected to http://0.0.0.0:3000/categories/77 
Completed 302 Found in 129ms (ActiveRecord: 21.8ms) 

回答

0

添加驗證方法的分類模型:

def properties_uniq? params 
    properties = params.map{ |_,property| property[:title] } 
    if properties.uniq.length < properties.length 
     errors.add(:category, "Properties duplication are not allowed.") 
     return false 
    end 
    true 
    end 

檢查屬性在控制器(當保存類別):

def create 
    @category = Category.new(category_params) 

    respond_to do |format| 
     if @category.save && @category.properties_uniq?(params[:category][:properties_attributes]) 
     format.html { redirect_to @category, notice: 'Category was successfully created.' } 
     format.json { render :show, status: :created, location: @category } 
     else 
     format.html { render :new } 
     format.json { render json: @category.errors, status: :unprocessable_entity } 
     end 
    end 
    end