Ruby on rails 4 RSpec+;工厂女孩&x2B;全球化bug?

Ruby on rails 4 RSpec+;工厂女孩&x2B;全球化bug?,ruby-on-rails-4,rspec,factory-bot,globalize,Ruby On Rails 4,Rspec,Factory Bot,Globalize,我在使用Globalize+RSpec+factory_girl时遇到了一个恼人的问题。 我有一个转换了属性的模型,当使用factory\u创建工厂时,就会出现问题。代码对此进行了完美的解释: 翻译迁移: def self.up CandidateBranch.create_translation_table!({ name: {type: :string, null: false, limit: 150 } }, { migrate_data: tr

我在使用Globalize+RSpec+factory_girl时遇到了一个恼人的问题。 我有一个转换了属性的模型,当使用factory\u创建工厂时,就会出现问题。代码对此进行了完美的解释:

翻译迁移:

  def self.up
    CandidateBranch.create_translation_table!({
      name: {type: :string, null: false, limit: 150 }
    }, {
      migrate_data: true
    })
  end
change_column_null :candidate_branches, :name, true
型号:

class CandidateBranch < ActiveRecord::Base
  translates :name
  ####### Validations ---------------------------------------------------------
  validates :name, presence: true, length: { in: 2..150 }

  ####### more code
end
测试:

日志:

Mysql事务:

   (0.3ms)  BEGIN
  CandidateBranch::Translation Load (0.5ms)  SELECT `candidate_branch_translations`.* FROM `candidate_branch_translations` WHERE `candidate_branch_translations`.`candidate_branch_id` = 1
   (0.4ms)  SAVEPOINT active_record_1
  SQL (0.6ms)  INSERT INTO `candidate_branches` (`id`, `created_at`, `updated_at`) VALUES (1, '2015-02-18 12:27:57.486623', '2015-02-18 12:27:57.486623')
Mysql2::Error: Field 'name' doesn't have a default value: INSERT INTO `candidate_branches` (`id`, `created_at`, `updated_at`) VALUES (1, '2015-02-18 12:27:57.486623', '2015-02-18 12:27:57.486623')
   (0.2ms)  ROLLBACK TO SAVEPOINT active_record_1
   (0.3ms)  ROLLBACK
   (0.3ms)  BEGIN
   (0.4ms)  SAVEPOINT active_record_1
  SQL (0.5ms)  INSERT INTO `candidate_branches` (`id`, `name`, `created_at`, `updated_at`) VALUES (1, 'trying out', '2015-02-18 12:29:09.195756', '2015-02-18 12:29:09.195756')
   (0.3ms)  RELEASE SAVEPOINT active_record_1
   (0.4ms)  ROLLBACK
如记录中所示,尽管在数据库查询中创建记录时定义了参数属性“name”,但转换表显然没有找到任何内容,然后尝试在未转换字段的情况下创建注册表,但失败了

然而,如果我们在模型中注释翻译语句

class CandidateBranch < ActiveRecord::Base
  #translates :name
  ####### Validations ---------------------------------------------------------
  validates :name, presence: true, length: { in: 2..150 }

  ####### more code
end


▶ RAILS_ENV=test bundle exec rspec spec/models/candidate_branch_spec.rb
"parameters => {:id=>1, :name=>\"trying out\"}"
"parameters => #<CandidateBranch id: 1, name: \"trying out\", created_at: \"2015-02-18 12:29:09\", updated_at: \"2015-02-18 12:29:09\”>"

是虫子吗?我做错什么了吗?这发生在其他人身上了吗?

我回答自己。Globalize不会填充表基模型中的转换字段,它所做的是将这些字段移动到它们的表转换中。这意味着必须对翻译后的字段应用属性验证,因为原始字段为空。

您自己发现,globalize会将翻译后的值移动到…\u translations表中。
您的错误消息是由基表上的
notnull
数据库约束引起的,该约束显然是由以前使用选项
NULL:false进行的迁移设置的

要使示例正常工作,您需要在迁移中添加以下内容:

  def self.up
    CandidateBranch.create_translation_table!({
      name: {type: :string, null: false, limit: 150 }
    }, {
      migrate_data: true
    })
  end
change_column_null :candidate_branches, :name, true

请你对这个问题多描述一下好吗。我不明白你的答案。
change_column_null :candidate_branches, :name, true