Some models require other models to be loaded first. But each required file only needs to be loaded once.
What is the best way to manage this? Put all the require lines in a file (like init.rb), or require files at the top of each model file?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s evaluate each option:
This means each individual file will be less cluttered, as
requires will all be in one place. However, it can happen that the order in which they are written matters, so you end up effectively doing dependency resolution manually in this file.Each file will have a little more content, but you won’t have to worry about ordering as each file explicitly requires the dependencies it needs. Calling
requirefor the same file multiple times has no effect.This also means that you can require only parts of your code, which is useful for libraries; e.g.
require active_support/core_ext/date/calculationsgets only the part of the library the external app needs.Of the two, I’d pick the second. It’s cleaner, requires less thinking, and makes your code much more modular.