23

In the config/application.rb file in a Rails app, there's the following section of code:

if defined?(Bundler)
    # If you precompile assets before deploying to production, use this line
    Bundler.require *Rails.groups(:assets => %w(development test))
    # If you want your assets lazily compiled in production, use this line
    # Bundler.require(:default, :assets, Rails.env)
end

I'm perhaps not clear what Bundler.require is doing. I was under the impression that it required the specified sections in the Gemfile, but I'm not clear as to why Bundler.require *Rails.groups(...) causes it to precompile and Bundler.require(...) causes assets to be lazily loaded.

Matty
  • 33,203
  • 13
  • 65
  • 93

1 Answers1

49

These lines don't actually change how your assets are used.

The first line,

Bundler.require *Rails.groups(:assets => %w(development test))

only loads gems from the assets group in your development and test environment. This means that things like sass-rails and uglifier won't be available in production, which then means that you won't be able to properly compile/minify/whatever your assets on the fly in production if you're making use of those gems.

On the other hand,

Bundler.require(:default, :assets, Rails.env)

will load the assets group in any environment, making those gems available in production to do asset compilation/minification/whatever on the fly.

So, as stated above, these lines don't actually change the behaviour of your asset pipeline - it simply means that you should use the first if you're going to precompile your assets for production, or use the second if you're going to lazily compile in production.

BaronVonBraun
  • 4,285
  • 23
  • 23
  • 3
    If your pre-compiling assets is there any reason not to just put the asset gems directly in Gemfile without a group if they are being included in every environment anyway? – Kris Nov 14 '11 at 18:35
  • 1
    @Kris basically it allows you to switch between the two strategies easily. If you want to switch, you won't need to move `gem ...` lines around – lulalala Jun 18 '12 at 09:25