2017-11-08 3 views
1

업데이트하는 방법 : share_count upsert으로 설정해야하나요? 나는 레일에 루비를 사용하고몽고이드에서 하나의 속성을 업데이트하는 방법

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

당신이 upsert''로 업데이트하는 방법을 알아 mongoid? –

+0

왜'upsert'를 사용하고 싶습니까? 'upsert'는 하나의 속성을 업데이트하는 것이 아니라 레코드를 갱신하거나 생성하는 것에 관한 것입니다. –

+0

감사합니다. –

관련 문제