我有一个模型,它有一个名为 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_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
任何帮助将不胜感激
最佳答案
我在本地试过这个,它似乎适用于 jsonb 字段:
FactoryGirl.define do
factory :product do
title "MyString"
category_paths { { some_key: some_value } }
end
end
希望对您有所帮助!
关于ruby-on-rails - 如何在工厂女孩中分配哈希值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32459873/