如何为开发和生产指定不同版本的gem

我需要有不同版本的gem用于开发和生产,所以我将以下内容放在我的gemfile中。

group :development, :test do gem 'rspec-rails', '2.11.0' gem 'bcrypt-ruby', '3.1.2' end group :production do gem 'rails_12factor' gem 'bcrypt-ruby', '3.0.1' end 

但如果我尝试bundle install甚至只是rails console我得到上述错误

我试过了

 bundle install --without production 

但我仍然收到错误消息。 仅供参考:我需要这样做,因为我要通过rails教程,在windows,ruby 2.0.0和bcrypt以及Heroku之间出现冲突,所以我在windows上使用bcrypt 3.1.2(对活动进行了修改)在Heroku上记录gemfile)和bcrypt 3.0.1。

有关更多详细信息,请参阅此内容: 在Windows上使用带有ruby2.0的bcrypt 3.0.1的问题

我基本上完成了第一个答案中提到的内容

编辑

 ################################################################### 

正如下面的答案所指出的,我真的应该在生产和开发中使用相同的版本(即使我只是在教程中工作)。 我最终做的是猴子修补ActiveModel使用

 gem 'bcrypt-ruby', '3.1.2' 

而不是

 gem 'bcrypt-ruby', '~> 3.0.0' 

在secure_password中。

我通过将以下内容放在lib / secure_password_using_3_1_2.rb中来完成此操作

 module ActiveModel module SecurePassword extend ActiveSupport::Concern module ClassMethods def has_secure_password # Load bcrypt-ruby only when has_secure_password is used. # This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library. #gem 'bcrypt-ruby', '~> 3.0.0' gem 'bcrypt-ruby', '3.1.2' require 'bcrypt' attr_reader :password validates_confirmation_of :password validates_presence_of :password_digest include InstanceMethodsOnActivation if respond_to?(:attributes_protected_by_default) def self.attributes_protected_by_default super + ['password_digest'] end end end end end end 

然后将以下内容添加到config / environment.rb

 require File.expand_path('../../lib/secure_password_using_3_1_2.rb', __FILE__) 

这个怎么样?

 gem "my_gem", ENV["RAILS_ENV"] == "production" ? "2.0" : "1.0" RAILS_ENV=production bundle 

简短的回答是你不能轻易做到这一点。 Bundler旨在强制所有gem在开发和生产之间使用相同的版本。 使用不同的版本可能会导致细微的错误。

你为什么不想在生产中运行3.1.2?

我知道我迟到了,但我无法在任何地方找到答案。

我试图找到这个问题的答案,因为我想部署到我的暂存环境的预发布gem和我的生产的完整gem版本。 我不希望我的生产环境在发布之前使用“1.0.2.pre1”之类的东西或类似的东西,因此版本为“1.0.2”。 原因是一个长篇故事:)

 version = "3.0.1" group :development, :test do version = "~> 3.1.2" end gem 'bcrypt-ruby', version 

如果你有dev / test组,它只运行块,它分配变量。