Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: What is the correct usage of Rails.root.join when pointing to "tmp/caching-dev.txt"?

I'm setting up a new Rails project and after giving it an initial tidy with Rubocop, I'm left with a single offense.

Rubop complains:

config/environments/development.rb:16:6: C: Please use Rails.root.join('path', 'to') instead.
  if Rails.root.join("tmp/caching-dev.txt").exist?

I see that Rails.root returns the path of the current project. So I've tried

if File.join(Rails.root, "tmp/caching-dev.text").exist?

instead. But still, Rubocop complains:

config/environments/development.rb:17:6: C: Please use Rails.root.join('path', 'to') instead.
  if File.join(Rails.root, "tmp/caching-dev.text").exist?
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What are the path and root arguments meant to be? Surely Rails.root is the path?!

like image 968
user3574603 Avatar asked Nov 09 '17 11:11

user3574603


2 Answers

IMHO, Pathname and / are not used enough.

if (Rails.root / 'tmp' / 'caching-dev.txt').exist?
like image 38
akim Avatar answered Sep 28 '22 20:09

akim


I think Rubocop is suggesting you to do something like this

if File.exist?(Rails.root.join('tmp', 'caching-dev.txt'))
like image 183
Ursus Avatar answered Sep 28 '22 18:09

Ursus