【问题标题】:Boolean data update with mongoid使用 mongoid 更新布尔数据
【发布时间】:2018-10-21 08:48:53
【问题描述】:

我使用 rails 5.2.1 和 mongoid 7.0.2

我尝试在用户模型中使用切换

class User
  include Mongoid::Document
  field :admin, type: Boolean, default: "false"
  has_many :categories

  def toggle!(field)
    send "#{field}=", !self.send("#{field}?")
    save :validation => false
  end
end

但是当我在控制台中尝试时,我遇到了这个错误:

irb(main):003:0> user.toggle!(:admin)
Traceback (most recent call last):
        2: from (irb):3
        1: from app/models/user.rb:7:in `toggle!'
NameError (undefined local variable or method `scope_stack' for Category:Class
Did you mean?  scopes)

【问题讨论】:

    标签: ruby-on-rails ruby mongoid


    【解决方案1】:
    class User
      include Mongoid::Document
      field :admin, type: Boolean, default: "false"
      has_many :categories
    
      def toggle!(field)
        self[field] = !self[field]
        self.save(validate: false)
      end
    end
    

    您是否仍然遇到与此实施相同的问题?

    【讨论】:

      猜你喜欢
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2013-07-22
      相关资源
      最近更新 更多