2017-11-08 6 views
1

更新方法:share_count upsertでこれを行う必要がありますか? は、私はあなたがupsert` `で更新する方法を知っていますかのRuby on Railsを使用してモンゴイドの属性を更新する方法

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 

答えて

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

をmongoid? –

+0

なぜ 'upsert'を使いたいのですか? 'upsert'は単一の属性を更新するのではなく、レコードを更新または作成することです。 –

+0

私はそれを得ました –

関連する問題