无法在测试环境中设置ActiveRecord ID

时间:2011-03-02 04:41:00

标签: ruby-on-rails-3

在我的before_create回调中, CAN 在生产和开发环境中分配我的自定义GUID。但是,在测试环境中运行的相同代码不能更改我的自定义GUID的值。请参阅下面的代码示例。

require 'rubygems'
require 'uuidtools'
require 'base32/crockford'

class WhatClass < ActiveRecord::Base
  # Declare the non-standard column guid as the primary key
  set_primary_key :guid
  # Use a callback to generate the record id
  before_create :create_uuid

  private
    # This method creates a universally unique identifier (UUID)
    # for an instance of WhatClass. This method is called before the
    # record is created.
    def create_uuid
      # Create the UUID
      uuid = UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, "string-to-create-from")
      self.guid = Base32::Crockford.encode(uuid.to_i)
    end
end

有人可以解释为什么self.guid在生产/开发中有效而在测试中没有?

0 个答案:

没有答案