Solve initializer is trying to connect db on rake assets:precompile
1 min readNov 29, 2018
Sometimes, you need load connection on db using initializer/ on Ruby on rails. For some environment like test and development it’s fine. But when we try to deploy on production which make lazy load turn on. it call to database. and throw message like this.
0m[91mMysql2::Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
To avoid this only on rake assets, we search where file located. than we skip using !defined?(selected_class)
Finally, when we running rake assets:precompile, we skip block code to execute.