对于唯一性的嵌套验证在rails 4项目中不起作用

时间:2014-09-01 10:51:19

标签: activerecord nested-forms nested-attributes ruby-on-rails-4.1

我有两个模型 - CategoryProperty与has_balongs_to_many关联。我使用nested_form gem。所以,Category有很多属性。当我创建新类别时,我可以创建属性。

类别模型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)

1 个答案:

答案 0 :(得分: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