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 7445009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:43:04+00:00 2026-05-29T11:43:04+00:00

I have a Post model that has a polymorphic association with a Vote model:

  • 0

I have a Post model that has a polymorphic association with a Vote model:

post.rb:

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

  belongs_to :user

  has_many :votes, :as => :votable, :dependent => :destroy 
end

vote.rb

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

  before_create :update_total

  protected

  def update_total
    self.votable.total_votes ||= 0
    self.votable.total_votes += self.polarity
  end
end

As you can see in vote.rb I want to accomplish the following:

Each time an instance of Vote is created, I want to update the total_votes column of the votable model instance (in this case an instance of Post).

But nothing happens, when I create a vote for a post with post.votes.create(:polarity => 1), the total_votes column of the post still being nil.

Any suggestions to fix this?

EDIT:

This didn’t work either:

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

  before_create :update_total

  protected

  def update_total
    self.votable.total_votes ||= 0
    self.votable.total_votes += self.polarity
    self.votable.save!
  end
end

schema.rb:

create_table "posts", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "title"
    t.integer  "comments_count", :default => 0, :null => false
    t.integer  "total_votes"
  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-29T11:43:04+00:00Added an answer on May 29, 2026 at 11:43 am

    When you create the Vote object, the Post object already exists. You update it, but you don’t save it. Try with the following code:

    def update_total
      self.votable.total_votes ||= 0
      self.votable.total_votes += self.polarity
      self.votable.save!
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a Post model that has a :title , :author , :content
I have a model that has counter_cache enabled for an association: class Post belongs_to
I have a typical, Post model: class Post< ActiveRecord::Base validates_presence_of :user_id #Line 1 validates_presence_of
I have a user and post model: class User < ActiveRecord::Base has_many :sent_posts, :class_name
I have a post model and an upvote model. upvote.rb class Upvote < ActiveRecord::Base
I have the model Post: class Post < ActiveRecord::Base has_one :location, :dependent => :destroy
I have two models, Article and Post that both inherit from a base model
I have a post controller that has many comments. The post model has a
I have a Post model object that has reference to a parent object. The
I have a model that looks something like this class Post(models.Model): id = models.IntegerField(unique=True,

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.