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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:02:33+00:00 2026-05-31T01:02:33+00:00

I want to create Thumbs Up vote system but I don’t know how to

  • 0

I want to create Thumbs Up vote system but I don’t know how to do it in best way.

Here’s my Entries#vote (controller/action):

def vote
    if Entry.where(id: params[:entry_id]).first && Vote.where(entry_id: params[:entry_id], user_id: @current_user.id).first.nil? # Check if entry with given entry_id exist && Check if user vote previously.
        Vote.create(entry_id: params[:entry_id], user_id: @current_user.id) # Create vote
        ActiveRecord::Base.connection.execute("UPDATE `entries` SET `entries`.`points` = `entries`.`points` + 1 WHERE `entries`.`id` = #{params[:entry_id].to_i}") # Update entry points count.
    end

    render nothing: true
end

I think it’s not optimal way to do it, because this action have lots of query. Here’s queries logs:

Started GET "/vote/2" for 127.0.0.1 at 2012-02-20 16:20:01 +0100
Processing by EntriesController#vote as JS
  Parameters: {"entry_id"=>"2"}
  ←[1m←[36mUser Load (1.0ms)←[0m  ←[1mSELECT id, name FROM `users` WHERE `users`.`auth_token` = '6f1aa3b944d530a1d52c6f40bcb69398' LIMIT 1←[0m
  ←[1m←[35mEntry Load (1.0ms)←[0m  SELECT `entries`.* FROM `entries` WHERE `entries`.`id` = 2 LIMIT 1
  ←[1m←[36mVote Load (0.0ms)←[0m  ←[1mSELECT `votes`.* FROM `votes` WHERE `votes`.`entry_id` = 2 AND `votes`.`user_id` = 1 LIMIT 1←[0m
  ←[1m←[35m (0.0ms)←[0m  BEGIN
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mINSERT INTO `votes` (`entry_id`, `user_id`) VALUES (2, 1)←[0m
  ←[1m←[35m (54.0ms)←[0m  COMMIT
  ←[1m←[36m (16.0ms)←[0m  ←[1mUPDATE `entries` SET `entries`.`points` = `entries`.`points` + 1 WHERE `entries`.`id` = 2←[0m
  Rendered text template (0.0ms)
Completed 200 OK in 115ms (Views: 1.0ms | ActiveRecord: 78.0ms)

Anybody have an idea how to do this in best way?

  • 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-31T01:02:34+00:00Added an answer on May 31, 2026 at 1:02 am

    That controller logic can be cleaned up. If it’s strictly just thumbs up where 1 vote = 1 point, you could use counter_cache to track points. If you also need thumbs down, then you could use update_counters instead.

    Entry.rb

    class Entry < ActiveRecord::Base
      has_many :votes
    end
    

    Vote.rb

    class Vote < ActiveRecord::Base
      belongs_to :entry, :counter_cache => :points  
      belongs_to :user
    end
    

    User.rb

    class User < ActiveRecord::Base
      has_many :votes
    end
    

    EntriesController#vote

    def vote
      Vote.find_or_create_by_entry_id_and_user_id(params[:entry_id], @current_user.id)
      render :nothing
    end
    

    For new vote SQL log is:

      Vote Load (0.3ms)  SELECT "votes".* FROM "votes" WHERE "votes"."entry_id" = 3 AND "votes"."user_id" = 1 LIMIT 1
       (0.1ms)  BEGIN
      SQL (0.4ms)  INSERT INTO "votes" ("created_at", "entry_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", Tue, 21 Feb 2012 16:51:54 UTC +00:00], ["entry_id", 3], ["updated_at", Tue, 21 Feb 2012 16:51:54 UTC +00:00], ["user_id", 1]]
      Entry Load (0.2ms)  SELECT "entries".* FROM "entries" WHERE "entries"."id" = 3 LIMIT 1
      SQL (0.2ms)  UPDATE "entries" SET "points" = COALESCE("points", 0) + 1 WHERE "entries"."id" = 3
       (2.3ms)  COMMIT
    

    For existing vote:

      Vote Load (0.4ms)  SELECT "votes".* FROM "votes" WHERE "votes"."entry_id" = 3 AND "votes"."user_id" = 1 LIMIT 1
     => #<Vote id: 7, user_id: 1, entry_id: 3, created_at: "2012-02-21 16:51:54", updated_at: "2012-02-21 16:51:54">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I build my action for create image thumbs, and I want add to the
I don't even know what questions I should ask. Well, I want to create
I was thinking of implementing a thumbs system, but mine would require a registration
I need to create slideshow for gallery. And one thing that I don't know
I want to create an XML string in Flex 3 in a similar way
i want create multiple search where statement $where_search is a multiple condition from post
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I want create an application with animate button? how can i do? after click
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =

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.