如何更新mongoid中的一个属性

How to update one attribute in mongoid

如何只更新 :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
count = SocialShares.total url, %w(sharedcount)
update_attributes share_count: count