Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:53:19+00:00 2026-05-31T03:53:19+00:00

I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names has_many

  • 0

I have a Post model:

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

a Tag model:

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

and a Vote model:

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

As you can see in this last model, it updates the :total_votes attribute of a post each time a new instance of Vote is created.

For some reason, this action is triggering the after_save :assign_tags action in the post model. So each time I create a vote for a post this part is called:

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

and all the tags are removed. I want the assign_tags to only be triggered when a post is being edited. Any suggestions to fix this?

EDIT:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:53:21+00:00Added an answer on May 31, 2026 at 3:53 am

    update post without callback inside vote model –

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

    OR

    you can use `update_column(name, value) it skips validations & callbacks –

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

    Here is revised vote model

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have the model Post: class Post < ActiveRecord::Base has_one :location, :dependent => :destroy
I have a Tag model: class Tag < ActiveRecord::Base has_many :taggings, :dependent => :destroy
I have a user and post model: class User < ActiveRecord::Base has_many :sent_posts, :class_name
I have the following Models: class Topic < ActiveRecord::Base has_many :posts, :dependent => :destroy
I have a typical, Post model: class Post< ActiveRecord::Base validates_presence_of :user_id #Line 1 validates_presence_of
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I have my models setup like so: class User < ActiveRecord::Base has_many :posts, :foreign_key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.