I
I
IvanN7772015-11-18 10:09:42
Ruby on Rails
IvanN777, 2015-11-18 10:09:42

I can't connect validation via lib, what's wrong?

I read an interesting article on Habr about how to make the model cleaner.
But since the article is a little old, it seems that something needs to be set up with pens.
Article for those who are interested habrahabr.ru/post/125822
Code section that caused difficulties

# lib/models/validations.rb
module Models
  module Validations
    # общий модуль c валидациями для нескольких моделей
  end
end

# lib/models/user/validation.rb
module Models
  module User
    module Validations
      # валидации для модели User
    end
  end
end

# app/models/user.rb
class User < ActiveRecord::Base
  include Models::Validations # Это я убрал за ненадобностью
  include Models::User::Validations
end

Does not find include Models::User::Validations
Tried to turn it on with handles
config.autoload_paths += %W(#{config.root}/lib/models)
config.autoload_paths += %W(#{config.root}/lib/models/**/)

But it didn't work.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
rutaka nashimo, 2015-11-18
@rutaka_n

And why did the standard dsl from ActiveRecord displease you? several lines of code that are directly related to the model should not be taken out of it.

A
Alexander Korolev, 2015-11-18
@kunashir

The last time I connected the code from lib, I also did
Yes, and in rails 4 you can use concerns for this.

V
vsuhachev, 2015-11-18
@vsuhachev

Autoload_paths specifies the root directory from which autoload starts searching for a file by module names.
Those. in your case:
In general, it is more logical to move your modules to app/validations. they are part of the application. And you don’t need to write anything to autoload, all app subdirectories are already included there by default

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question