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

时间:2021-09-20 07:10:12

I need to have different versions of a gem for development and production, so I put the following in my gemfile.

我需要开发和生产不同版本的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

but if i try to do bundle install or even just rails console I get the above error

但是如果我尝试捆绑安装或者仅仅是rails控制台,我就会得到上面的错误

I have tried

我有试过

bundle install --without production

but I still get the error message. FYI: I need to do this because I am going thru the rails tutorial and there is a conflict that arises between windows, ruby 2.0.0 and bcrypt and Heroku so I am using bcrypt 3.1.2 on windows (with a modification to the active record gemfile) and bcrypt 3.0.1 on Heroku.

但我还是得到了错误信息。FYI:我需要这样做,因为我要通过rails教程,在windows、ruby 2.0.0和bcrypt和Heroku之间出现了冲突,所以我在windows上使用bcrypt 3.1.2(对active record gemfile进行了修改),在Heroku上使用bcrypt 3.0.1。

See this for more details: Issues using bcrypt 3.0.1 with ruby2.0 on Windows

有关更多细节,请参见此内容:在Windows上使用ruby2.0使用bcrypt 3.0.1的问题。

I basically did what is mentioned in the first answer

我基本上做了第一个答案中提到的

EDIT

编辑

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

As the answer below points out, I really should be using the same version in both production and development (even tho I am just working thur a tutorial). What I ended up doing is monkey patching ActiveModel to use

正如下面的答案所指出的,我确实应该在生产和开发中使用相同的版本(尽管我只是在学习教程)。我最后做的是用猴子补活字

gem 'bcrypt-ruby', '3.1.2'

rather than

而不是

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

in secure_password.

在secure_password。

I accomplished this by placing the following in lib/secure_password_using_3_1_2.rb

为此,我在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

and then adding the following to config/environment.rb

然后在config/environment.rb中添加以下内容

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

3 个解决方案

#1


2  

How about this?

这个怎么样?

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

RAILS_ENV=production bundle

#2


1  

The short answer is that you can't do that easily. Bundler is intended to enforce that all gems are using the same version between development and production. Using different versions can lead to subtle bugs.

简而言之,你不可能轻易做到这一点。Bundler旨在加强所有gems在开发和生产之间使用相同的版本。使用不同的版本会导致细微的错误。

Why don't you want to run 3.1.2 in production?

为什么不运行3.1.2版本呢?

#3


0  

I know I'm late to the party, but i couldn't find an answer anywhere.

我知道我参加聚会迟到了,但我到处都找不到答案。

I was trying to find an answer to this question as i wanted to deploy to a prerelease gem to my staging environment and a full gem version to my production. I didn't want my production environment to use anything like "1.0.2.pre1" or anything like that until it was released, thereby having the version "1.0.2". The reason is a long story :)

我试图找到这个问题的答案,因为我想将它部署到预发行版gem到我的登台环境中,并将完整的gem版本部署到我的产品中。我不希望我的生产环境使用“1.0.2”之类的东西。1或类似的东西,直到它被发布,因此有版本“1.0.2”。原因说来话长。

version = "3.0.1"

group :development, :test do
    version = "~> 3.1.2"
end

gem 'bcrypt-ruby', version

It just runs the block if you have the dev/test group, which assigns the variable.

如果您有一个dev/test组,它就会运行这个块,这个组会分配变量。

#1


2  

How about this?

这个怎么样?

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

RAILS_ENV=production bundle

#2


1  

The short answer is that you can't do that easily. Bundler is intended to enforce that all gems are using the same version between development and production. Using different versions can lead to subtle bugs.

简而言之,你不可能轻易做到这一点。Bundler旨在加强所有gems在开发和生产之间使用相同的版本。使用不同的版本会导致细微的错误。

Why don't you want to run 3.1.2 in production?

为什么不运行3.1.2版本呢?

#3


0  

I know I'm late to the party, but i couldn't find an answer anywhere.

我知道我参加聚会迟到了,但我到处都找不到答案。

I was trying to find an answer to this question as i wanted to deploy to a prerelease gem to my staging environment and a full gem version to my production. I didn't want my production environment to use anything like "1.0.2.pre1" or anything like that until it was released, thereby having the version "1.0.2". The reason is a long story :)

我试图找到这个问题的答案,因为我想将它部署到预发行版gem到我的登台环境中,并将完整的gem版本部署到我的产品中。我不希望我的生产环境使用“1.0.2”之类的东西。1或类似的东西,直到它被发布,因此有版本“1.0.2”。原因说来话长。

version = "3.0.1"

group :development, :test do
    version = "~> 3.1.2"
end

gem 'bcrypt-ruby', version

It just runs the block if you have the dev/test group, which assigns the variable.

如果您有一个dev/test组,它就会运行这个块,这个组会分配变量。