【问题标题】:Attribute was supposed to be an Array, but was a String属性应该是一个数组,但它是一个字符串
【发布时间】:2017-02-14 13:26:35
【问题描述】:

我的数据库中有一个文本字段序列化为一个数组:

class EmailMessage < ActiveRecord::Base
  serialize :from, Array

在保存 ActiveRecord 对象之前,我给方法赋值:

@email_message.from = [current_user.email]

但对象不保存。相反,我得到一个错误:

Attribute was supposed to be an Array, but was a String. -- "user@gmail.com"

为什么会发生这个错误?我以为我将一个数组传递给该方法。我该如何解决这个问题?

【问题讨论】:

标签: ruby-on-rails


【解决方案1】:

尝试添加to_yaml:

@email_message.from = [current_user.email].to_yaml

另外,如果您还没有,请将from: [] 添加到您的强参数中。

这有时是一种常见的疏忽。

【讨论】:

    猜你喜欢
    • 2013-05-14
    • 2018-04-29
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多