Hello, I am working on a voting system. I wish to have a system that allows users to vote every X amount of time. I am using a vote model that creates a new record every time a vote is cast. How would i add a check to stop users form voting more than once in the X amount of time. here is the vote model class CreateVotes < ActiveRecord::Migration def self.up create_table :votes do |t| t.column :time, :datetime t.column :user_id, :int t.column :celebrity_id, :int end end def self.down drop_table :votes end end thanks for your time reading this post!!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stewart wrote:> How would i add a check > to stop users form voting more than once in the X amount of time. > > here is the vote model > > class CreateVotes < ActiveRecord::Migration > def self.up > create_table :votes do |t| > t.column :time, :datetime > t.column :user_id, :int > t.column :celebrity_id, :int > end > end > > def self.down > drop_table :votes > end > endYou can do this with validations. Write a custom validation that checks the database to see if any other votes conflict with it. class Vote < ActiveRecord::Base def validate if Vote.find(:first, :conditions => [''time < ? AND user_id = ?'', 1.day.ago, user_id] errors.add_to_base ''You cannot vote more than once per day!'' end end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Alex, Thanks mate thats exactly what i was looking for. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ok one more question. My site uses sessions to track all of the user information. This session is saved in a value called current_user. When a vote is cast i want to record the user that cast the vote so i can check and make sure that user is not voting more than once in a day. I also wish to support guest voting. This would record the ip address of the voting user. Should i set this logic up in the model? thanks again for your time -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stewart wrote:> ok one more question. > > My site uses sessions to track all of the user information. This session > is saved in a value called current_user. When a vote is cast i want to > record the user that cast the vote so i can check and make sure that > user is not voting more than once in a day. I also wish to support guest > voting. This would record the ip address of the voting user. Should i > set this logic up in the model? > > thanks again for your timesorry to bump this but this is a very busy forum -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stewart wrote:> > > Hi Alex, > > Thanks mate thats exactly what i was looking for.Also, if you call the column created_at, Rails will automatically fill in the time for you. Alan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stewart wrote:> ok one more question. > > My site uses sessions to track all of the user information. This session > is saved in a value called current_user. When a vote is cast i want to > record the user that cast the vote so i can check and make sure that > user is not voting more than once in a day. I also wish to support guest > voting. This would record the ip address of the voting user. Should i > set this logic up in the model? > > thanks again for your timeBusiness logic always goes in the model. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---