Activerecord 唯一性的嵌套验证在rails 4项目中不起作用
我有两个模型-类别和属性与许多关联。我使用嵌套形式的gem。所以,类别有很多属性。当我创建新类别时,我可以创建属性 类别模型Category.rb: 正如您所看到的,属性模型中存在唯一性:真正的验证。 当我试图使用rails控制台或在“类别编辑”页面上创建相同的属性时,它会给出类似于错误的属性,该属性的名称已经存在。这是正确的,应该是正确的 但在“类别新建”页面上,当我创建与您在屏幕截图中看到的相同属性时,它不会给我错误,验证不起作用,它会创建具有两个相同属性的新类别。。。。发生了什么?请帮忙 下面是日志:Activerecord 唯一性的嵌套验证在rails 4项目中不起作用,activerecord,nested-forms,nested-attributes,ruby-on-rails-4.1,Activerecord,Nested Forms,Nested Attributes,Ruby On Rails 4.1,我有两个模型-类别和属性与许多关联。我使用嵌套形式的gem。所以,类别有很多属性。当我创建新类别时,我可以创建属性 类别模型Category.rb: 正如您所看到的,属性模型中存在唯一性:真正的验证。 当我试图使用rails控制台或在“类别编辑”页面上创建相同的属性时,它会给出类似于错误的属性,该属性的名称已经存在。这是正确的,应该是正确的 但在“类别新建”页面上,当我创建与您在屏幕截图中看到的相同属性时,它不会给我错误,验证不起作用,它会创建具有两个相同属性的新类别。。。。发生了什么?请帮忙
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)
将验证方法添加到类别模型:
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
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)
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