Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Using AJax - Prevent users from creating multiple friendships

There is a delay from when I click Add Friend until the javascript runs and sends the friend request. The user can click the Add Friend button repeatedly during this time resulting in multiple friend requests. Is there any way to prevent this.

2 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Great question, Matthew! You can add a uniqueness validation that's scoped to the friend id in the user friendship model. Check out the rails guide on the topic for more info:

http://edgeguides.rubyonrails.org/active_record_validations.html

That worked! Thanks Jason.

Here's the code I added

validates :user_id, uniqueness: true