Getting the business logic right!

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!!!

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
end

You 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

Hi Alex,

Thanks mate thats exactly what i was looking for.

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 time

sorry to bump this but this is a very busy forum

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

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

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 time

Business logic always goes in the model.