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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:07:14+00:00 2026-06-09T18:07:14+00:00

I have the following code in my Rails 3 application: def like @suggestion =

  • 0

I have the following code in my Rails 3 application:

 def like
    @suggestion = Suggestion.find(params[:id])
    Suggestion.update_all("votes = (votes + 1)")
    redirect_to suggestions_url
  end

  def dislike
    @suggestion = Suggestion.find(params[:id])
    Suggestion.update_all("votes = (votes - 1)")
    redirect_to suggestions_url
  end

It’s working, but rather than updating the current suggestion it’s updating them all. So I changed it to:

 def like
    @suggestion = Suggestion.find(params[:id])
    @suggestion.update_all("votes = (votes + 1)")
    redirect_to suggestions_url
  end

  def dislike
    @suggestion = Suggestion.find(params[:id])
    @suggestion.update_all("votes = (votes - 1)")
    redirect_to suggestions_url
  end

but then I get:

undefined method `update_all' for #<Suggestion:0x007f87c2b918a0>

So then I tried @suggestion.update_attribute(:votes, '1') but that resets the value to 1 instead of incrementing it.

What’s the correct way to achieve this? I just want the integer (votes) of the current suggestion to increment/decrease by 1 on each save.

I’ve also tried the following with no luck:

def like
 @suggestion = Suggestion.find(params[:id])
 @suggestion.increment(:votes)
 redirect_to suggestions_url
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-06-09T18:07:15+00:00Added an answer on June 9, 2026 at 6:07 pm

    A couple things. It sounds like what you want is a controller action that increments an attribute by one. You were probably closest with the code

    @suggestion.update_attribute(:votes, '1')
    

    If you check the documentation for that method, it sets the value of the attribute votes to the second arguement, the string '1', on the object, @suggestion, and its corresponding row in the database. Instead of setting it to '1', you want to set it to the incremented value:

    @suggestion.update_attribute(:votes, @suggestion.votes + 1)
    

    Ethan suggested using the convenience method, increment!, which works just the same.

    Now, if you wanted to actually auto-increment each time the object gets saved (as in something else about the object gets altered, you’d want to use the :before_save callback with increment without the bang.

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

Sidebar

Related Questions

I have the following code in my Rails application's routes file: MyApp::Application.routes.draw do constraints
I have the following code in one of my Rails 3 application views: <ul>
In my Rails 3.2.3 application I have the following code in the application.html.erb to
I have the following code in my Rails 3 application. @clinical_healthplan_spend = Clinical.select(ClientKey as
I have the following rails code in a loop that has a link for
i have the following code in the rails company model: class Company include Mongoid::Document
We have the following code working for a complex rails form with checkboxes. I'm
In my Rails project I have the following code: auto_link( h( wrap_long_string(post.text,50) )).gsub(/\n/,<br />)
I'm having an issue with rails involving javascript. Basically, I have the following code:
I have the simple following code, which is working in a ruby (not rails)

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.