在注册过程中,我有一个用户模型和租户模型。最近我在Tenant模型中添加了一个序列化列,我可以更新这个专栏。但是,在创建新租户时,我设计了通过嵌套参数创建租户,并收到以下错误:
ActiveRecord::SerializationTypeMismatch (Attribute was supposed to be a Hash, but was a String. -- "{}")
:重要的是要注意我在注册过程中没有触及该列,我尝试将该列包含在消毒剂上,但它也是如此。在架构上有一个默认值“{}”。下面是一些代码:
create_table "tenants", force: :cascade do |t|
t.string "tenant_name"
t.string "tenant_address"
t.string "tenant_city"
t.string "tenant_zip"
t.string "tenant_phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "authorized"
t.boolean "trial"
t.string "plan_id"
t.string "plan_name"
t.string "braintree_id"
t.string "subscription_id"
t.jsonb "preferences", default: "{}", null: false
t.string "tenant_state"
t.string "tenant_country"
t.index ["preferences"], name: "index_tenants_on_preferences", using: :gin
端
class Tenant < ApplicationRecord
has_many :users, :dependent => :delete_all
has_many :customers, :dependent => :delete_all
has_many :work_orders, :dependent => :delete_all
has_many :vehicles, :dependent => :delete_all
has_many :suppliers, :dependent => :delete_all
end
serialize :preferences, Hash
store_accessor :preferences, :state_tax, :mun_tax, :welcome_sms, :estimate_sms, :completed_sms, :disclaimer
以下是我的用户控制器的一部分:
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
def new
build_resource({})
self.resource.tenant = Tenant.new
respond_with self.resource
end
# POST /resource
def create
super
if @user.save
@result = Braintree::Customer.create(
:first_name => @user.name,
:last_name => @user.lastname,
:company => @user.tenant.tenant_name,
:email => @user.email,
:phone => @user.phone
)
if @result.success?
@user.tenant.set_braintree_id(@result.customer.id)
flash[:notice] = 'Thanks you! and Welcome to Autokick.tech enjoy your free 30 days!'
else
flash[:notice] = @result.errors
end
end
end
答案 0 :(得分:0)
t.jsonb "preferences", default: "{}", null: false
默认值为字符串"{}"
,如错误所示
将其更改为default: {}
,不带引号。