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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:01:32+00:00 2026-06-03T20:01:32+00:00

I’m trying to use this statement: @vote = Vote.where(:resource_id => params[:id], :user_id => current_user.id).first_or_create(:value

  • 0

I’m trying to use this statement:

@vote = Vote.where(:resource_id => params[:id], :user_id => current_user.id).first_or_create(:value => 1)

This is for an upvote or a downvote. If the row doesnt exist in the DB it should create it (ive theyve never voted on that post), otehrwise it should modify the value with -1,0,1.

If they click upvote a second time it needs to ‘undo’ the vote and put the row “value” at 0 instead of 1. The same happens with a downvote but this is only the upvote function right now.

I’m having trouble debugging this statement though in rails. I need to basically differentiate between whether its a saved record or a created record. If i put:

@vote = Vote.where(:resource_id => 25, :user_id => 1)
@vote.exists?

I get true

but if i put this.. (and the record DOES exist, it just worked above)..

@vote = Vote.where(:resource_id => 25, :user_id => 1).first_or_create(:value => 1)
@vote.exists?

I get…
NoMethodError: undefined method `exists?’ for #

After looking at the classes for the two objects i see one is a “Vote” and one is an “ActiveRecord::Relation”.

My ultimate goal is to somehow refactor this (mostly the part where it checks if the vote value is already 1, if so set it to zero (undo the vote) otherwise put it at 1

This is the code before I started trying to refactor with first_or_create. its long and ugly.

 if @vote.exists?
      # Check if they have already voted
      if @vote.first.value == 1
        @vote.first.value = 0
      else
        @vote.first.value = 1
      end
      flash[:notice] = "previous vote modified"
      @vote.first.save
    else
      # Create a new Vote instance, save it
      @vote = Vote.new(:resource_id => params[:id], :user_id => current_user.id, :value => 1)
      if @vote.valid?
        @vote.save
        flash[:notice] = "New vote added"
      else
        @vote.save
        flash[:notice] = (@vote.errors.full_messages).to_s
      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-03T20:01:33+00:00Added an answer on June 3, 2026 at 8:01 pm

    I would instead use the Rails magic methods find_or_create_by methods.

    Check out ‘Dynamic Attribute-Based Finders’ here:

    http://api.rubyonrails.org/classes/ActiveRecord/Base.html

    You use it like a dynamic finding method, but then pass in extra variables you want to add to create the Vote if it doesn’t exist.

    In your case, it’d be something like:

    @vote = Vote.find_or_create_by_resource_id_and_user_id(25, 1, :value => 1)
    

    If you don’t want it to immediately create, you can use find_or_initialize_by which essentially creates a Vote.new without saving.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

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.