24

as freshman, I come across many unclear details. One of those is bin-thing. I've been wondering what's the difference between:

rails generate... 

and

bin/rails generate...

? They seem to behave the same when I run those commands in console. There is also rake and bin/rake... and many more probably. Thanks for help.

-- greetings

zombie_ghast
  • 1,713
  • 15
  • 21
  • there's no difference, except that `rails` or `rake` may not be present in path for some reason and then you can run them from `bin` directory. You can read about path here http://stackoverflow.com/a/16560612/2422778 – Mike Szyndel Aug 15 '14 at 22:24

2 Answers2

29

If you just run 'rails', RubyGems will activate the latest version of the rails executable it can find in PATH. This is fine as long as you use this version of Rails in your project. If you have a project which uses an older version of Rails and you run 'rails', you can run into problems when trying to run code that's changed in the latest Rails version. Binstubs fix this problem by making sure your environment uses the versions specified in your project's Gemfile.

Ahmed Ali
  • 671
  • 7
  • 10
6

Consider this scenario:

I had an app that was using Rails version 4.0.0. My goal was to upgrade it to Rails 4.1.9. To do so, I tried to upgrade it step by step: first, upgrading to 4.0.13, then 4.1 and finally 4.1.9.

Everything went smoothly. All the tests were passing using RSpec.

Finally, I tried to run my server with rails s. Booom! Ruby crashed. Then, I used bin/rails s. Everything went ok.

So I think if you have different version of rails set up in your system, it's safer to use bin/rails option.

P.S. To make sure that my assumption is correct, I removed all rails version except 4.1.9 and then I tried to rerun the server with rails s. No crash this time.

Hope this clarifies.

Dave Qorashi
  • 326
  • 2
  • 11
  • for me this is not 100% clear. It used to work for me a lot of years using rails. A couple of months ago I realized that I had to use `bin/rails` for some projects. In a current project I e.g. can't use my custom generator when running `rails g`. It is only visible when using `bin/rails g`. Any ideas why this is? – basiszwo Jul 24 '17 at 08:54