SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    LENZ book -- limit users to 1 vote

    anyone -- know how to limit the user to 1 vote.

    I cannot believe this book did not include the code for this.

    I'm assuming that the code belongs in the story controller...somewhere here:

    def vote

    @story = Story.find(params[:id])
    @story.votes.create ( :user => @current_user)

    I'm new...I googled everything, and have found nothing.

    I'm assuming it would be an if /else statement...checking to see if the story has a user id that matches the current user id. If so, the vote should not be counted.

    Any help would be great. It's a good book. But not including code like this makes this book incomplete.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can (and should use validations [1]:

    Code ruby:
    class Vote < ActiveRecord::Base
      validates_uniqueness_of :user_id, :scope => :story_id
    end

    You may need to change the names according to your database schema.

    [1] http://rails.rubyonrails.com/classes...s.html#M000957

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for this!

    I will try this tonight and let you know the outcome.

    Hopefully, this code can help other beginners.

  4. #4
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks!

    I tried this and it worked!

    Thanks a lot for your help and providing the link!!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •