Anton Ipatov
Anton Ipatov

Reputation: 167

How to update one attribute in mongoid

How to update only :share_count, maybe this should be done with upsert ? I use ruby on rails and mongoid

  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

Upvotes: 1

Views: 1514

Answers (1)

Nikita Misharin
Nikita Misharin

Reputation: 2030

count = SocialShares.total url, %w(sharedcount)
update_attributes share_count: count

Upvotes: 1

Related Questions