FactoryGirl:违反非空约束

FactoryGirl: not-null constraint violation

尝试使用 FactoryGirl.create(:place) 创建地点实例但出现以下错误:

 Failure/Error: FactoryGirl.create(:place)
 ActiveRecord::StatementInvalid:
   PG::NotNullViolation: ERROR:  null value in column "town_city" violates not-null constraint
   DETAIL:  Failing row contains (1, 2017-07-14 13:32:41.246627, 2017-07-14 13:32:41.246627, 51.476900, 0.000010, Greenwich Observatory, Greenwich, London, null, 24849 Lesch Garden, null, null, null, MF4 1FO, null, null, null, null, 1, null, f).
   : INSERT INTO "places" ("created_at", "geocode_address", "latitude", "longitude", "postcode", "region_id", "street_address", "updated_at") VALUES (, , , , , , , ) RETURNING "id"
 # ./spec/requests/api/v1/events_spec.rb:5:in `block (2 levels) in <top (required)>'

无法弄清楚为什么,因为在我的工厂中我明确设置了 town_city:

放置工厂:

factory :place, traits: [:pageable] do
  town_city 'my hardcoded value'
  latitude { rand(50.0...53.0) }
  longitude { rand(-2.0...2.0) }
  street_address { Faker::Address.street_address }
  postcode { Faker::AddressUK.postcode }
  association :region, factory: :region
end

页面和可分页特征:

trait :pageable do
  association :page, strategy: :build
end

factory :page do
  sequence(:title) { |n| "#{Faker::Product.product_name}-#{n}" }
  association :user
  association :cover_image, factory: :image
  telephone { Faker::PhoneNumber.phone_number }
  description { generate :paragraphs_seq }
end

地区:

factory :region do
  level 'area'
end

任何想法可能是什么原因?

事实证明,我的 Place 模型中有 before_validation 挂钩,它试图重置 town_city 使用区域字段数据。所以我想我必须修改我的区域工厂