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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:08:28+00:00 2026-06-02T13:08:28+00:00

The basic outline of my code is this: Check to see if a user

  • 0

The basic outline of my code is this:

  1. Check to see if a user has a “vote” object associated with a particular “report”

  2. If there is no such vote object, then create one.

  3. Assign it a value (upvote vs. downvote)

  4. If there IS such a vote object, then change the existing vote’s value.

Here’s the problem though…The program never FINDS the vote object even when it (apparently) exists in the database!

My code looks like this:

    def vote_up # voting code
      @was_new = false

      current_report = Report.find(params[:id])
      @report = current_report

      begin
        @vote = current_report.votes.find_by_user_id(current_user.id) #<<---HERE IS THE BUG!!
      rescue
        @was_new = true
        @vote = Vote.create(:user_id => current_user.id, :votable_type => 'Report', :value => 0)   # default vote score of zero
      end

      current_report.votes << @vote
      current_report.save

      if @was_new #if the vote was new...
        @report.upvotes += 1
        @vote.value = 1

      elsif !@was_new and @vote.value == -1 #else, if you are changing your vote...

        @report.upvotes += 1
        @report.downvotes -= 1
        @vote.value = 1

      end

        @vote.save
        @report.save

        redirect_to(report_path(@report))
    end

The error I receive is this:

 SQLite3::SQLException: no such column: votes.votable_id: SELECT  "votes".* FROM "votes"  WHERE "votes"."votable_id" = 3 AND "votes"."votable_type" = 'Report' AND "votes"."user_id" = 1 LIMIT 1

I feel like the solution is simple, like writing @vote = current_report.votes.find(params[:user_id], :as => :user_id) or something like that.

EDIT:

I got it working. Here’s the working code:

 def vote_up # voting code

 @exists = false

 get_vote(1)

 if !@exists #if the vote was new...
  @report.upvotes += 1

elsif @exists and @vote.value == -1 #else, if you are changing your vote...

  @report.upvotes += 1
  @report.downvotes -= 1
  @vote.value = 1

    end

    @vote.save
    @report.save

 redirect_to(report_path(@report))

end

def vote_down

 @exists = false

    get_vote(-1)

    if !@exists # this should handle vote changing
  @report.downvotes += 1

  elsif @exists and @vote.value == 1

  @report.downvotes += 1
  @report.upvotes -= 1
  @vote.value = -1

    end

    @vote.save
    @report.save

redirect_to(report_path(@report))

end

 def get_vote(val) # voting code
 current_report = Report.find(params[:id])

 @report = current_report
 @vote = current_report.votes.find_by_user_id(current_user.id) 

 unless @vote # create a vote only if it's nil
   @vote = Vote.create(:user_id => current_user.id, :votable_id => 3, :votable_type => 'Report',  :value => val) # default vote score of zero
   current_report.votes << @vote
  current_report.save
 else #if it's not nil
  @exists = true
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-06-02T13:08:30+00:00Added an answer on June 2, 2026 at 1:08 pm

    Check the Dynamic attribute-based finders. You should be able to do something like: (untested code)

    def vote_up # voting code
      current_report = Report.find(params[:id])
      @report = current_report
    
      # gets the vote or initialize a new one for that user_id
      @vote = current_report.votes.find_or_initialize_by_user_id(current_user.id) 
    
      if !@vote.saved?
        @vote.votable_type = 'Report' 
        @vote.value = 0
        #  current_report.votes << @vote #not sure if it's necessary
        #  current_report.save #not sure if it's necessary
    
        @report.upvotes += 1
        @vote.value = 1
      elsif @vote.value == -1 
        @report.upvotes += 1
        @report.downvotes -= 1
        @vote.value = 1
      end
    
      @vote.save
      @report.save  
    
      redirect_to(report_path(@report))
    end
    

    IMPORTANT: And one more thing. You should port most of this method to your Report model, it’s clearly a business thing, so there is where this should go!

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

Sidebar

Related Questions

I've just wrote some code for a basic outline of a page. It for
Does anybody have a guide to this, containing code samples, tips and an outline
So heres the basic outline function x(){ // some code function y(){ //some more
I'm searching for an algorithm for Digit summing. Let me outline the basic principle:
Basic doubt...If QTP generates vbscript code as we record actions, can't we directly write
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
This basic menu works perfectly in all modern browsers but is not playing ball
First off, please accept my apologies if this question is basic, I mainly have
Im pretty sure this is really basic. However I have no knowledge of Perl
I am aware this is a very basic question, we are very new to

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.