Friday, March 12, 2010

require file in rails environment

by Sandip Ransing 0 comments
Tags


There are different ways to load particular file in rails application environment if the required file exists.
1. Using RAILS_ROOT

if File.exists?(file=File.join(RAILS_ROOT,'config', 'initializers', 'smtp_gmail.rb'))
  require file
end
2. Using Rails.root & join
require Rails.root.join('config', 'initializers', 'smtp_gmail.rb')
3. Using Rails.root, join & exists? method.
require file if File.exists?(file = Rails.root.join('config', 'initializers','smtp_gmail.rb'))
4. Using File and direct path to file
require  File.join('root','app','config', 'initializers', 'smtp_gmail.rb')
Among all above methods of  loading file, Using Rails.root, join & exists? method seems to be pretty good to have.
Any improvements are most welcome!

blog comments powered by Disqus

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter