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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:29:36+00:00 2026-06-01T13:29:36+00:00

I have vote and votedown tables for posts. I can validate uniqueness within the

  • 0

I have vote and votedown tables for posts. I can validate uniqueness within the vote and votedown models to ensure that users cant vote or votedown more than once. I’d like to validate uniqueness across the both models so that a user cant votedown a post that theyve already voted up or vice versa. Ive tried the custom validation, userfaved, defined below but its not working.

 class Vote < ActiveRecord::Base

 validates_uniqueness_of :user_id,  :scope => :article_id #allows only one vote

 validate :userfaved

 def userfaved
  if Votedown.where( :user_id => :user_id, :artcle_id => :article_id).any?
      errors.add(:user_id, 'already voted on this') 
  end
 end



class Votedown < ActiveRecord::Base

validates_uniqueness_of :user_id, :scope => :article_id #allows only one votedown

validate :userfaved

def userfaved
    if Vote.where(:user_id=> :user_id, :article_id => :article_id).any?
        errors.add(:user_id, 'already voted on this')
    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-01T13:29:38+00:00Added an answer on June 1, 2026 at 1:29 pm

    It looks like there’s a bug in your validation: the values in the hash you’re passing to where are symbols. Change it to the following and it should work:

    Votedown.where( :user_id => self.user_id, :article_id => self.article_id).any?
    

    But I think there’s a simpler way to do this. Could you just have one table Votes and add an attribute to it that records whether it’s up or down? That would make your uniqueness check very simple. If you really want separate classes for it, you could use single table inheritance: have a votes table, a superclass Vote, and two subclasses, VoteUp and VoteDown.

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

Sidebar

Related Questions

I have two tables -- an article table and a vote table. Users can
I have a system where registered users can vote up/vote down comments for a
I have a vote button I created that is contained within a .vote_div. 2
I've built several apps in rails that have submissions that you can vote on.
I have an website where people can place a vote like this: http://mysite.com/vote/25 This
Let's say I have two simple models project t.string :title vote t.references :project t.integer
I have three models: Post , Comment , User and Vote . I'm using
Let's say I have some models: User , Post , and Vote . A
I have a (many) .dgp files, which is an image file that I can't
I have two tables : VOTE with fields pk:id, fk:uid, date and SUBMISSION with

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.