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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:50:21+00:00 2026-06-07T20:50:21+00:00

I have three models User , Post , Vote I need to doesn’t allow

  • 0

I have three models User, Post, Vote

I need to doesn’t allow user to vote for his own post.
How I can make this in my models and test this in Rspec?

Post Model:

class Post < ActiveRecord::Base
  attr_accessible :title, :main_text, :video, :photo, :tag

  validates :title,     presence: true, length:  {minimum: 1, maximum: 200}
  validates :main_text, presence: true, length:  {minimum: 1}

  belongs_to :user
  has_many   :votes

end

User Model:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :bio

  has_many :posts
  has_many :votes

  validates :name,  presence: true, length: {minimum: 1, maximum: 120}
  validates :email, presence: true, length: {minimum: 5, maximum: 250}, uniqueness: true, 
                    format: {:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i}


end

Vote model:

class Vote < ActiveRecord::Base

  attr_accessible :user_id, :post_id, :units

  belongs_to :user
  belongs_to :post

  validates :post_id, uniqueness: {scope: :user_id}  #does not allow user to vote for the same post twice


end

My spec test for Vote:

require 'spec_helper'

describe Vote do

  it "does not allow user to vote for the same post twice" do 
    user = User.create(name: "Nik", email: "nik@google.com" )
    post = Post.create(title: "New Post", main_text: "Many, many, many...")
    vote1 = Vote.create(user_id: user.id, post_id: post.id)  
    vote1.errors.should be_empty    
    vote2 = Vote.create(user_id: user.id, post_id: post.id)        
    vote2.errors.should_not be_empty      
  end 

  it "does not allow user to vote for his own post" do
    user = User.create(name:"Nik", email:"a@a.ru")
    post = Post.create(user_id: user.id, title: "New Post", main_text: "Many, many, many...")
    vote1 = Vote.create(user_id: user.id, post_id: post.id)
    vote1.errors.should_not be_empty
  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-07T20:50:22+00:00Added an answer on June 7, 2026 at 8:50 pm

    I’ve not tested the following code so it could not work or even kill your cats but try with a custom validation with add an error if the vote’s user is the same of the post’s user.

    Note I return if the user or the post are nil for obvius reasons.

    # In your vote model
    validate :users_cant_vote_their_posts
    
    def users_cant_vote_their_posts
      return if user.nil? or post.nil?
      if user_id == post.user_id
        errors[:base] = "A user can't vote their posts"
      end
    end
    

    EDIT: Here a possible test, here I’m using FactoryGirl to generate votes. Again this code is not tested (sorry for the pun)

    describe Vote do
      subject { vote }
    
      let!(:vote) { FactoryGirl.build(:vote) }
    
      it 'from factory should be valid' do
        should be_valid
      end
    
      context 'when user try to double vote' do
        before do
          # Create another vote from user to post
          FactoryGirl.create(:vote, :user => vote.user, :post => vote.post)
        end
    
        it { should_not be_valid }
      end
    
      context 'when user try to vote his posts' do
        before do
          # Set the user whom voted to the post's owner
          vote.user = vote.post.user
        end
    
        it { should_not be_valid }
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: Post , Comment , User and Vote . I'm using
Right now, I have three models Post, Comment and User (using Devise ) associated
I have the models User, Post, Comment and Tag. User creates Post. Posts can
I have three models: User Image Group . User belongs to an image; Group
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have three models: Post ( has_many votes as votable ), Comment ( has_many
I have a couple of models in my system: User Reputation Post Reputation Response
I have a rails app which currently has 3 models: Post , User and
I've run into this problem with testing. Let's assume I have two models, User

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.