rails - 关注验证的问题

时间:2015-02-03 13:40:35

标签: ruby-on-rails validation activesupport-concern

我对问题的验证有疑问 有班级 /app/models/group.rb

class Group < AbstractModel
  include Localized::Title

  ...

end

/app/models/concerns/localized/title.rb

module Localized::Title
  extend ActiveSupport::Concern
  include ActiveModel::Validations

  include do 
    validates :title_ua, length: {minimum: 3, maximum: 200}, uniqueness: true
    validates :title_ru, length: {minimum: 3, maximum: 200}, uniqueness: true
  end

...

end

当我尝试在模型中使用验证时,它起作用,但不关心。 请帮帮我,我做错了什么?

P.S。 AbstractModel&lt;的ActiveRecord :: Base的, rails 4.2,ruby 2.2.0p0

1 个答案:

答案 0 :(得分:9)

它包含'不包含'。试试这样:

module Localized::Title
  extend ActiveSupport::Concern
  include ActiveModel::Validations

  included do 
    validates :title_ua, length: {minimum: 3, maximum: 200}, uniqueness: true
    validates :title_ru, length: {minimum: 3, maximum: 200}, uniqueness: true
  end

...

end