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