2012-03-10 28 views
1

我有一個型號:在創建投票時觸發post_sheet中的after_save回調?

class Post < ActiveRecord::Base 
    attr_accessible :title, :content, :tag_names 

    has_many :votes, :as => :votable, :dependent => :destroy 
    has_many :taggings, :dependent => :destroy 
    has_many :tags, :through => :taggings 

    attr_writer :tag_names 
    after_save :assign_tags 

    def tag_names 
    @tag_names || tags.map(&:name).join(" ") 
    end 

    private 

    def assign_tags 
    self.tags = [] 
    return if @tag_names.blank? 
    @tag_names.split(" ").each do |name| 
     tag = Tag.find_or_create_by_name(name) 
     self.tags << tag unless tags.include?(tag) 
    end 
    end 
end 

一個標籤型號:

class Tag < ActiveRecord::Base 
    has_many :taggings, :dependent => :destroy 
    has_many :posts, :through => :taggings 
    has_many :subscriptions 
    has_many :subscribed_users, :source => :user, :through => :subscriptions 

    def tag_posts_count 
    "#{self.name} (#{self.posts.count})" 
    end 
end 

投票型號:

class Vote < ActiveRecord::Base 
    belongs_to :votable, :polymorphic => true 
    belongs_to :user 

    before_create :update_total 

    protected 

    def update_total 
    total_average = self.votable.total_votes 
    self.votable.update_attribute(:total_votes, total_average + self.polarity) 
    end 
end 

正如你可以在此看到最後一個型號,它更新了:total_votes a每次創建投票的新實例時,都會發布帖子的屬性。

出於某種原因,此操作正在觸發後期模型中的after_save :assign_tags操作。所以每次我創建一個投票的這個部分被稱爲:

def assign_tags 
     self.tags = [] // I added this so that the previous array of tags are removed before the new ones are added. 

並且所有的標籤都被刪除。我只想在編輯帖子時觸發assign_tags。任何建議來解決這個問題?

編輯:

votes_controller:

class VotesController < ApplicationController 
    def vote_up 
    @votable = params[:votable_type].constantize.find(params[:id]) 

    if @votable.votes.exists?(:user_id => current_user.id) 
     @notice = 'You already voted' 
    else 
     @vote = @votable.votes.create(:user_id => current_user.id, :polarity => 1) 
     @votable.reload 
    end 

    respond_to do |format| 
     format.js 
    end 
    end 

回答

1

更新後沒有回調裏面vote模型 -

def update_total 
    self.votable.total_votes += self.polarity 
    self.votable.send(:update_without_callbacks) 
    end 

OR

,你可以用`update_column(名稱,值),它跳過驗證&回調 -

def update_total 
    self.votable.update_column(:total_votes, votable.total_votes + polarity) 
end 

下面是修改後的投票模式

class Vote < ActiveRecord::Base 
    belongs_to :votable, :polymorphic => true 
    belongs_to :user 

    after_create :update_total 

    protected 

    def update_total 
    if votable && votable.is_a?(Post) 
     self.votable.update_column(:total_votes, votable.total_votes + self.polarity) 
    end 
    end 
end 
+0

對不起,我有點困惑。你是否告訴我刪除'update_total'中的所有代碼並用上面的代碼替換它? (我試過,但'total_votes'沒有更新)。 – alexchenco 2012-03-10 08:48:55

+0

它應該工作..看看編輯答案 – 2012-03-10 09:04:22

+0

感謝您的幫助。我試過了,得到這個錯誤:'NoMethodError(undefined method' update_without_callbacks'for#): app/models/vote.rb:11:'update_total' app/controllers/votes_controller.rb:8 :在'vote_up'' 。我在**編輯**中添加了'votes_controller.rb'。也許有些東西可以讓你瞭解這個問題。 – alexchenco 2012-03-10 09:09:09

相關問題