ruby-on-rails – 如何在工厂女孩中分配哈希?

我有一个名为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

希望有所帮助!

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...