Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Mongoid:保存字符串数组抛出TypeError?_Ruby_Mongodb_Mongoid - Fatal编程技术网

Ruby Mongoid:保存字符串数组抛出TypeError?

Ruby Mongoid:保存字符串数组抛出TypeError?,ruby,mongodb,mongoid,Ruby,Mongodb,Mongoid,我做错了什么?我有一个基于Mongoid的模型: class ReadableThing include Mongoid::Document field :shop, type: Integer field :user, type: Integer field :title, type: String field :s_title, type: String field :s_d

我做错了什么?我有一个基于Mongoid的模型:

class ReadableThing
  include Mongoid::Document
  field :shop,    type: Integer
  field :user,                 type: Integer
  field :title,                type: String
  field :s_title,              type: String
  field :s_desc,               type: String
  field :is_part,              type: Boolean
  field :pieces,               type: Array

  def self.with_shop_only(shop, with_parts)
    ReadableThing.where(shop_id: shop, user: nil, is_part: with_parts)
  end

  def self.with_shop_and_user(shop, user_id, with_parts)
    ReadableThing.where(brokerage_shop_id: shop, user: user_id, is_part: with_parts)
  end
end
运行此代码时:

ReadableThing.create({
  shop: 2,
  user: nil,
  title: "Ooohh samples",
  s_title: "SPL",
  s_desc: nil,
  is_part: true,
  pieces: ["this","is","a","sample"],
})
我得到这个错误:

TypeError: no implicit conversion of String into Integer
我已经能够追踪到“碎片”阵列,没有它,一切都很完美。不过,我不知道我做错了什么


谁能帮我一下吗

不使用“类型”也不能解决任何问题。保存新的
可读对象后,您是否能够将项目添加到
片段
数组中?例如,在您成功保存了一个新的
rt=ReadableThing.create({…})
之后,您能否执行
rt.pieces=[“this”、“is”、“a”、“sample”]
rt.save
?这行得通吗?你的模型有没有你没有展示的验证?@MikeSlutsky做一个单独的rt.pieces=[“这个”、“是”、“一个”、“样本”]行得通,是的。我还是想用ActiveRecord的方式来做though@muistooshort不,不是真的,不。似乎在抄写时我错过了。它被纠正了,它仍然在发生,我现在也在想为什么这会是一个问题。