新手问题 mongoid 如何把一个 field 置为空

dxwts · December 02, 2013 · Last by dxwts replied at December 05, 2013 · 2044 hits

我使用 mongoid 时,有个 type 为 string 的字段设置值后要更新为空,可是怎么操作都是前一次设置的值,这是为什么啊?

model字段定义和action操作

  • model 的 validation 限制
  • action没有 save
  • ......

#1 楼 @dddd1919

  include Mongoid::Document

  field :name, :type => String
  field :column, :type => String, default: ""

  validates :name, :column, presence: true
  validates_uniqueness_of :name

  belongs_to :user
end

每次更新时我按下面进行操作,如果@columns不为空操作都正常,我清空的话@columns设为""就无法保存 @custom_table = user.custom_table.find_by(:name => @name) @custom_table.column = @columns user.custom_table << @custom_table

validates :name, :column, presence: true column 当然不能设为 ""

#3 楼 @HungYuHei 哦,非常感谢二位

如果遇到 model 相关数据错误问题,在最后puts @custom_table.errors.messages一定会有错误提示信息。 再有 @custom_table.column = @columns 后应该加上 ·@custom_table.save

You need to Sign in before reply, if you don't have an account, please Sign up first.