我有一个模型,其中有一个名为category_paths
的字段。在 postgres 中它是JSONB
类型。
当我使用 factory_girl 设置category_paths
时,factory_girl 会将值类型更改为 String
。即使我分配了一个Hash
,也会被更改为String
,请考虑以下代码:
FactoryGirl.define do
factory :product do
title "MyString"
after(:build) do |p|
p.category_paths = Hash.new
puts p.category_paths.class # This prints as String
end
end
end
这很奇怪,我无法弄清楚发生了什么。在Rails控制台中尝试时可以正常工作。问题仅在factory中使用时出现。这是factory_girl的工作方式吗?还是有一种方法来控制这种行为?
这是产品模型:
class Product < ActiveRecord::Base
acts_as_copy_target
searchkick autocomplete: ['brand'], callbacks: :async
scope :search_import, -> { includes(:product_offers) }
has_many :product_offers, autosave: true
validates :title, presence: true
validate :validate_category_paths
end
任何帮助都将不胜感激