Answer the question
In order to leave comments, you need to log in
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
include Models::User::Validations
config.autoload_paths += %W(#{config.root}/lib/models)
config.autoload_paths += %W(#{config.root}/lib/models/**/)
Answer the question
In order to leave comments, you need to log in
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.
The last time I connected the code from lib, I also did
Yes, and in rails 4 you can use concerns for this.
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 questionAsk a Question
731 491 924 answers to any question