rails - problems with validation in concern
Asked Answered
E

1

6

I have problems with validation in concern There is class
/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

When I try to use validation in model it's works, but not in concern. Help me please, what I do wrong?

P.S. AbstractModel < ActiveRecord::Base, rails 4.2, ruby 2.2.0p0

Extempore answered 3/2, 2015 at 13:40 Comment(0)
I
18

it's 'included' not include. Try it like this:

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
Idol answered 3/2, 2015 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.