Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - problems with validation in concern

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

like image 761
greenif Avatar asked Feb 03 '15 13:02

greenif


1 Answers

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
like image 157
goma Avatar answered Nov 08 '22 10:11

goma