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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:26:29+00:00 2026-05-18T02:26:29+00:00

I have a Rails app where users can follow one another. I’ve just discovered

  • 0

I have a Rails app where users can follow one another. I’ve just discovered a bug where a user can follow a person more than once.

If a user isn’t following someone, and opens the website in two different windows and clicks ‘follow’ in both windows, they are following the person twice.

In my controller I have a condition which I thought would prevent this from happening but it doesn’t work.

  def follow
   unless follows_arr.include?(params[:id])
    @r = current_user.relationships.build
    @r.friend_id = params[:id]
    @r.save
   end
  end

follows_arr is an array of the IDs of the users the current user is following (current_user.relationships.map{|u| u[:friend_id]} ). params[:id] is the ID of the person the user is trying to follow.

For some reason, the second time someone clicks follow in this situation, the controller isn’t checking the updated database and two of the same ‘relationship’ are created.

Am I doing something wrong here? Is there another way to go about this?

Thanks!

  • 1 1 Answer
  • 2 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-18T02:26:30+00:00Added an answer on May 18, 2026 at 2:26 am

    The “real” answer to your problem is that those types of constraints should be checked in the model, not the controller (one may even argue it should be checked at database level, but Rails is just not made that way.

    You can easily use the validates_uniqueness_of method in your model, e.g.:

    class Relationship < ActiveRecord::Base
       belongs_to follower
       belongs_to followed_user
    
      validates_uniqueness_of :followed_user, :scope => :follower
    end
    

    Which would indicate that each that the followed_user must be unique per follower.

    As for your original code, it is hard to say what is going wrong. It may depend on where the follows_arr comes from – maybe it contains some cached data, or you cached the variable yourself somewhere.

    But while it will be a useful learning experience figuring out why the original solution doesn’t work, for solving your original problem you’ll be better of putting the code in the model where it belongs.

    If you need to do something in the controller, you can check like this:

    def follow
       ...
       if current_user.save
          # In this case everything was saved all right
       else
          # the user couldn't be saved for some reason (validation failed)
       end
     end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app where users can upload images. After they upload an
I have a Rails app with Users, and each user HABTM Roles. I want
I'm building a Rails 3 app where users can select one of a number
Using Rails 3.1.3. I have Accounts and Users. One Account can have many Users.
I have a Rails 3 app that lets users enter comments. I want any
I have a Rails app (running 3.2.2) and I want to cache my users'
I have a Rails 3.1.3 app which uses devise for users authentication and soft-deletes
I have a Rails app, with 2 models : a User model and a
I have a Rails 3.1 app with a User model and a Venue model.
So I am using Devise for user authentication in my rails app. I have

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.