2017-02-02 28 views
0

在註冊過程中,我有一個用戶模型和Tenant模型。最近,我在Tenant模型中添加了一個序列化列,我可以更新此列。但是,當創建新租戶時,我設計了通過嵌套參數創建租戶,並且出現以下錯誤: ActiveRecord::SerializationTypeMismatch (Attribute was supposed to be a Hash, but was a String. -- "{}"):重要的是要注意,我在註冊過程中未觸摸該列我曾嘗試在清潔劑中包括該列,但它也是這樣。在架構上有一個默認值是'{}'。下面的一些代碼:使用Devise嵌套參數模型失敗註冊已序列化的列

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

t.jsonb "preferences", default: "{}", null: false

默認爲一個字符串"{}"類似錯誤說。
將其更改爲default: {}而不加引號。