【问题标题】:How to update one attribute in mongoid如何更新 mongoid 中的一个属性
【发布时间】:2018-04-21 00:15:19
【问题描述】:

如何只更新 :share_count,也许这应该用 upsert 来完成? 我在 rails 和 mongoid 上使用 ruby​​

  class FeedEntry
  include Mongoid::Document
  require 'social_shares'

  field :name, type: String
  field :url, type: String
  field :share_count, type: Integer

  before_save :load_shares

  def load_shares
    self.share_count = SocialShares.total url, %w(sharedcount)
  end

  def self.update_shares_today
    @feed_entries = FeedEntry.today.all
    @feed_entries.each do |feed_entry|
      feed_entry.update
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails mongodb mongoid


    【解决方案1】:
    count = SocialShares.total url, %w(sharedcount)
    update_attributes share_count: count
    

    【讨论】:

    • 为什么要使用upsertupsert 是关于更新或创建记录,而不是关于更新单个属性
    猜你喜欢
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多