简体   繁体   English

Rails 自定义验证方法

[英]Rails custom validate method

I'm using the ancestry gem in order to create subcategories.我正在使用祖先 gem来创建子类别。 After that when I create an item (item model) I use the following group select in order to associate the item with the subcategories it belongs to.之后,当我创建一个项目(项目模型)时,我使用以下组 select 将项目与其所属的子类别相关联。 The group select has the categories and subcategories grouped together. select 组具有组合在一起的类别和子类别。

<%= form.input :category, collection: @categories, as: :grouped_select, group_method: :children, label: false, include_blank: true %>

@categories = Category.where(ancestry: nil).order('name ASC')

Also I'm using the the cocoon gem in order to create and thus associate many subcategories to an item.此外,我正在使用茧宝石来创建许多子类别并将其关联到一个项目。

Now I would like to add a custom validate method inside a model which will allow the user to only add subcategories that belong to the same main category, or otherwise return an error:现在我想在 model 中添加一个自定义validate方法,这将允许用户仅添加属于同一主类别的子类别,否则会返回错误:

errors.add(:category, "you can only choose subcategories from he same category")   

I'm kind of stuck on how to create this validate method.我有点坚持如何创建这个validate方法。 Maybe first I should find the subcategories that are being added:也许首先我应该找到正在添加的子类别:

subcategory = Category.find(category)

And then find the category that the subcategory belongs to with this:然后用这个找到子类别所属的类别:

subcategory.root.name

But after that I have now idea what to do.但在那之后,我现在知道该怎么做。

How can I create this validate method in order to allow the user to only add subcategories that belong to the same main category, or otherwise return an error?如何创建此validate方法以允许用户仅添加属于同一主类别的子类别,或者返回错误?

Thank you in advance.先感谢您。 Help would be very much appreciated on this one.非常感谢您对此提供帮助。

If I understand, all Categories assigned to an Item must be subcategories of the same Category.如果我理解,分配给项目的所有类别都必须是同一类别的子类别。 And I assume they have to be subcategories.我认为它们必须是子类别。

Write a custom validation method in Item which verifies all the subcategories have parents, and all the subcategories are the same.在 Item 中编写一个自定义验证方法,验证所有子类别都有父类别,并且所有子类别都相同。

class Item < ApplicationRecord
  has_many categories

  validate :categories_have_the_same_parent

  private def categories_have_the_same_parent
    if categories.any? { |cat| !cat.ancestry_id }
      errors.add(
        :categories,
        :not_a_subcategory,
        "All categories must be sub-categories"
      )
    end

    if categories.each_cons(2).any? { |a,b| a.ancestry_id != b.ancestry_id }
      errors.add(
        :categories,
        :different_ancestors,
        "All categories must have the same parent."
      }
    end
  end
end

The diagnostics on this can be improved to include the offending categories.可以改进对此的诊断以包括违规类别。

Then, in the form, populate the Item with the form information and check the Item's validation as normal.然后,在表单中,使用表单信息填充 Item 并正常检查 Item 的验证。

class ItemController < ApplicationController
  def create
    @item = Item.create!(...params...)

    ...creation succeeded...
  rescue ActiveRecord::RecordInvalid => e
    ...creation failed...
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM