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

  • Home
  • SEARCH
  • 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 986271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:17:40+00:00 2026-05-16T05:17:40+00:00

I’m calling update_attributes but it doesn’t seem to be changing the object. def add_to_vote_count(increment)

  • 0

I’m calling update_attributes but it doesn’t seem to be changing the object.

  def add_to_vote_count(increment)
    vc = vote_count ? vote_count : 0
    puts "CALLED a_t_v_c(#{increment}), NEW VOTE COUNT SHOULD BE #{vc + increment}"
    self.update_attributes(:vote_count => (vc + increment))
  end

Here’s some console testing:

ruby-1.8.7-p299 > p = Factory(:playlist)
 => #<Playlist id: 56, user_id: 0, message: "Lorem ipsum dolor sit amet, consectetur adipisicing...", title: "Ipsum Dolor", flag: 2, created_at: "2010-08-12 18:18:51", updated_at: "2010-08-12 18:18:51", moderation_score: 0, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, category_id: nil, widget_id: 22100001, permalink: #<ActiveSupport::Multibyte::Chars:0x102feba48 @wrapped_string="ipsum-dolor-27">, cached_tag_list: "", vote_count: 0> 

ruby-1.8.7-p299 > p.vote_count
 => 0 
ruby-1.8.7-p299 > p.votes
 => [] 
ruby-1.8.7-p299 > p.votes << Vote.new(:vote => true)
CALLED a_t_v_c(1), NEW VOTE COUNT SHOULD BE 1


VOTE CREATED

 => [#<Vote id: 235, vote: true, voteable_id: 56, voteable_type: "Playlist", voter_id: nil, voter_type: nil, created_at: "2010-08-12 18:19:09", updated_at: "2010-08-12 18:19:09">] 

ruby-1.8.7-p299 > p.votes
 => [#<Vote id: 235, vote: true, voteable_id: 56, voteable_type: "Playlist", voter_id: nil, voter_type: nil, created_at: "2010-08-12 18:19:09", updated_at: "2010-08-12 18:19:09">] 

ruby-1.8.7-p299 > p.vote_count
 => 0 

ruby-1.8.7-p299 > p
 => #<Playlist id: 56, user_id: 0, message: "Lorem ipsum dolor sit amet, consectetur adipisicing...", title: "Ipsum Dolor", flag: 2, created_at: "2010-08-12 18:18:51", updated_at: "2010-08-12 18:18:51", moderation_score: 0, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, category_id: nil, widget_id: 22100001, permalink: #<ActiveSupport::Multibyte::Chars:0x102feba48 @wrapped_string="ipsum-dolor-27">, cached_tag_list: "", vote_count: 0> 

Any idea why nothing is getting saved? I don’t get any errors, and calling the methods normally returns the ‘correct’ results.

ruby-1.8.7-p299 > p.update_attributes(:vote_count => 100)
 => true 
ruby-1.8.7-p299 > p.vote_count
 => 100 
ruby-1.8.7-p299 > p.add_to_vote_count(10)
CALLED a_t_v_c(10), NEW VOTE COUNT SHOULD BE 110
 => true 
ruby-1.8.7-p299 > p.vote_count
 => 110

The only difference I can see is that add_to_vote_count is being called by the Vote class in its after_create method. As you can see from the output, though, add_to_vote_count is definitely getting called.

  #in vote.rb
  def after_create
    voteable.add_to_vote_count( vote ? 1 : -1 )
    puts "\n\nVOTE CREATED\n\n"
  end

Edit: Actually, it turns out that the object is getting updated, but my reference is not. That is to say, p returns the old version with no votes, but Playlist.find(p.id) returns the correct one. I assume this is due to caching (Rails doesn’t want to hit the database again for an item it should have in memory), so how do I force Rails to realize that stuff changed?

  • 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-16T05:17:41+00:00Added an answer on May 16, 2026 at 5:17 am

    I really don’t know if this is going to help you, but you can use increment instead of update_attributes.

    def add_to_vote_count(increment)   
       self.increment!(:vote_count, increment)
    end
    

    If you try, please let me know if this worked :]

    Edit

    I believe the reload method can solve your problem!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.